简体   繁体   中英

I want to know how to find a special character in a string

I am programming in Java in Eclipse, I want to ask users to enter their specific ID, Which starts with an uppercase G and has 8 digit numbers. like G34466567. if the user enters an invalid ID it will be an error. how can i separate a valid ID from others?

You can use regex. This pattern checks if the first character is a capital G and there are 8 digits following:

([G]{1})([0-9]{8})$

As you see there are two expressions which are separated by the () . The first says "only one character and this one has to be a capital G". And the second one says, there have to be 8 digits following and the digits can be from 0 to 9.

Every condition contains two "parts". The first with the [] defines which chars are allowed. The pattern inside the {} show how many times. The $ says that the max length is 9 and that there can't be more chars.

So you can read a condition like that:

([which chars are allowed]{How many chars are allowed})
^------------------------\/---------------------------^
                    One condition

And in Java you use it like that:

String test= "G12345678";
boolean isValid = Pattern.matches("([G]{1})([0-9]{8})$", test);

As you see that matches method takes two parameters. The first parameter is a regex and the second parameter is the string to check. If the string matches the pattern, it returns true.

Create an ArrayList. Ask the user to input the ID, check if it is already there in the list, ignore, otherwise add that ID to the list.

EDIT: For ensuring that the rest 8 characters of the String ID are digits, you can use the regex "\\\\d+" . \\d is for digits and + is for one or more digits.

Scanner sc = new Scanner(System.in);
ArrayList<String> IDS = new ArrayList();
char more = 'y';
String ID;
String regex = "\\d+";
while (more == 'y') {
    System.out.println("Pleaes enter you ID.");
    ID = sc.next();
    if (IDS.contains(ID)) {
        System.out.println("This ID is already added.");
    } else if (ID.length() == 9 && ID.charAt(0) == 'G' && ID.substring(1).matches(regex)) {
        IDS.add(ID);
        System.out.println("Added");
    } else {
        System.out.println("Invalid ID");
    }
    System.out.println("Do you want to add more? y/n");
    more = sc.next().charAt(0);
}

Assuming that you save the id as a string, you can check the first letter and then check if the rest is a number.

Ex.

String example = "G12345678";
String firstLetter = example.substring(0, 1); //this will give you the first letter
String number = example.substring(1, 9);  //this will give you the number

To check that number is a number you could do the following instead of checking every character:

try {
      foo = Integer.parseInt(number);
} catch (NumberFormatException e) {
      //Will Throw exception!
      //do something! anything to handle the exception.
      // this is not a number
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM