So I cannot figure this out for the life of me. I am trying to write a program that prompts the user to enter a phone number. This will be entered as a string and converted to an array of integers later on in the program. However, the situation I am running into now is validating that the string entered by the user ONLY!!! contains digits from 2-9. I have tried the .contains method as well as the .match method, however using these always provides me with false results. If anyone could please shed some light on how to solve this I would greatly appreciate it. Thanks in advanced.
Here is what I have so far:
Scanner user_input = new Scanner(System.in);
String number = user_input.nextLine();
if(number.contains("[2-9]+")) {
for(int count = 0; count < number.length(); count++) {
digits[count] = number.charAt(count)-'0';
}
System.out.println(Arrays.toString(digits));
//printWord(digits, out, length, 0);
} else {
System.out.println("Invalid phone number!");
System.exit(0);
}