简体   繁体   English

检查字符串是否包含字母

[英]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. 包含2到9之间的数字。 I have tried the .contains method as well as the .match method, however using these always provides me with false results. 我已经尝试过.contains方法和.match方法,但是使用这些方法总是给我带来错误的结果。 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 您可以创建诸如onlyDigits(String)类的方法,并在提供的正则表达式中使用Pattern

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. 您可以将正则表达式与Pattern类一起使用,该类具有Pattern.matches(String regex, CharSequence input)方法。 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]+匹配一组合法字符2、3,...,9中至少一个字符的序列
  • [2-9]* would match 0 or more occurrences of characters in that set [2-9]*将匹配该集中出现的0个或更多字符
  • [2-9]{n} would match n occurences [2-9]{n}将匹配n

number.matches("\\\\d+"); will return true if all the characters are digits; 如果所有字符都是数字,则将返回true; an empty string will return false 一个空字符串将返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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