检查字符串是否包含整数或字符a-z以外的任何内容

Jer*_*emy 1 java

这是处理java启动的问题.我的目标是获取一个Vin编号并将其存储到一个字符串中.确保它不超过9个字符.此外,Vin编号仅包含数字和字母az或AZ.如果用户输入无效的Vin编号,它应该循环回来并提示输入新的Vin编号.我的问题是告诉字符串是否包含整数或字母以外的任何内容.我已经看过这个Utility.IsAlphaNumeric方法,但我无法弄清楚如何实现它,或者它是否甚至是一个java方法.每当我尝试使用它时,NetBeans都会给我错误.

private static String checkVin() {
    Scanner input = new Scanner(System.in);
    String vinNumber = "";
    do {
        System.out.println("Please enter the vin number of the car");
        vinNumber = input.next();
        if (vinNumber.length() > 9) {
            System.out.println("invalid input for Vin number");
        }
        for (int i = 0; i < vinNumber.length(); i++) {
            char currentCharacter = vinNumber.charAt(i);

            if ((currentCharacter < '0' || currentCharacter > '9')
                    || !( currentCharacter < 'a' || currentCharacter > 'z')
                    || !(currentCharacter > 'A' || currentCharacter > 'Z'));
            System.out.println("invalid input for Vin Number");
            break;
        }
        return vinNumber;

    } while (true);
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*uel 10

Utility 不是核心库中的类的名称.

您可以使用替换整个检查循环

import java.util.regex.Pattern;
import java.util.regex.Matcher;

private static final Pattern VALID_VIN_NUMBER = Pattern.compile(
    "[0-9a-zA-Z]{1,9}");

public static boolean isVin(String s) {
  return VALID_VIN_NUMBER.matcher(s).matches();
}
Run Code Online (Sandbox Code Playgroud)