0

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);
}
Brandon
  • 49
  • 1
  • 11
  • use regular expression instead. – cychoi Nov 08 '14 at 04:59
  • 1
    @cychoi He's attempting to. What he doesnt know is that `contains` doesnt accept regex – Vince Nov 08 '14 at 05:01
  • see this: http://stackoverflow.com/questions/10575624/java-string-see-if-a-string-contains-only-numbers-and-not-letters/10575676#10575676 – cychoi Nov 08 '14 at 05:04
  • 1
    If `number.matches("[2-9]+")` doesn't work for you, then provide an example which fails. – Tom Nov 08 '14 at 05:04
  • I have tried that. I used !number.contains("[a-zA-Z]"). I entered the following "a757454" it ran as though there was no letter entered. I did a system.out.println and noticed it stored the 'ASCII' value of 'a'. I think this may be the problem, however I am unsure how to solve this. Thanks – Brandon Nov 08 '14 at 05:06
  • how about `"^[2-9]+$"`? – cychoi Nov 08 '14 at 05:07
  • `I have tried that.` That seems to be not true. You've tried something different. Try what I've posted and come back with numbers which failed your test. – Tom Nov 08 '14 at 05:08
  • 2
    Yea haha I am stupid got it working now. Sorry for that I was using the contains method when I should have used the matches method. Thank you all for your feedback. – Brandon Nov 08 '14 at 05:09

5 Answers5

0

You might create a method like onlyDigits(String) and use a Pattern with your provided regex like

private static Pattern pattern = Pattern.compile("[2-9]+");

public static boolean onlyDigits(String in) {
    Matcher m = pattern.matcher(in);
    return m.matches();
}

Then you can call it like

public static void main(String[] args) {
    System.out.println(onlyDigits("123"));
    System.out.println(onlyDigits("123a"));
    System.out.println(onlyDigits("234"));
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Try this:

String regex = "[2-9]+";
number.matches(regex);
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
Abdul Rehman Janjua
  • 1,461
  • 14
  • 19
0

You could check to make sure the input given is within the range of characters, for example:

Scanner user_input = new Scanner(System.in);
String number = user_input.nextLine();

for(int i=0; i<number.length;i++){
   if(number.charAt(i) >= '2' && number.charAt(i) <= '9') {
        digits[i] = number.charAt(i)-'0';
    }
} else {
    System.out.println("Invalid phone number!");
    System.exit(0);
   }
}
//printWord(digits, out, length, 0);
System.out.println(Arrays.toString(digits));
Mauricio Trajano
  • 2,677
  • 2
  • 20
  • 25
0

You can use regular expressions with the Pattern class, which has the Pattern.matches(String regex, CharSequence input) method. You can do something like

public static void main(String[] args) {
    System.out.println("Enter phone number");
    Scanner input = new Scanner(System.in);
    String phoneNumber = input.nextLine();                            
    System.out.println(Pattern.matches("[2-9]+", phoneNumber));
}

You can choose a regular expression that fits your needs. For example,

  • [2-9]+ above matches a sequence of at least one character in the set of legal characters 2, 3, ..., 9
  • [2-9]* would match 0 or more occurrences of characters in that set
  • [2-9]{n} would match n occurences
juh
  • 238
  • 4
  • 6
0

number.matches("\\d+"); will return true if all the characters are digits; an empty string will return false

Buddy
  • 10,874
  • 5
  • 41
  • 58
Abel ANEIROS
  • 6,029
  • 2
  • 25
  • 19