0

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?

  • Welcome on Stackoverflow. Please show us some attempts you've already tried. – Christian Oct 20 '15 at 15:44
  • I thought it's better to convert the string (the ID) to an array.first check the array[0] and make sure that it is a G , and then check others to make sure they are numbers. because i am beginner i don't know how to have these as syntax . – Mahsa Chenari Oct 20 '15 at 15:50

3 Answers3

2

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.

Christian
  • 22,585
  • 9
  • 80
  • 106
1

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);
}
CodeRunner
  • 687
  • 5
  • 13
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
}
user
  • 2,015
  • 6
  • 22
  • 39
  • I would use example.charAt(0) instead of example.substring(0, 1) – FredK Oct 20 '15 at 16:17
  • This doesn't check if there are more than 9 chars. – Christian Oct 20 '15 at 16:20
  • @FredK according to this: http://stackoverflow.com/questions/18201191/what-is-the-best-way-to-get-the-first-letter-from-a-string-in-java-returned-as it is better to do a substring – user Oct 20 '15 at 16:21
  • @Christian true, but she did not seem to be asking for that. Anyway, just add if number.length() is 8 – user Oct 20 '15 at 16:23
  • @user79303 Whether to use charAt or a substring depends on what you are going to do with it. The reference you cited was using it to pass to a method that needed a string, not a char, so in that instance the substring is preferred. – FredK Oct 20 '15 at 16:29