简体   繁体   中英

Checking if string contains letters

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);
}

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"));
}

Try this:

String regex = "[2-9]+";
number.matches(regex);

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));

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

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

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