Java 检查用户是否只在字符串中输入字母

Java 检查用户是否只在字符串中输入字母,java,Java,我是Java新手,我想知道如何检查一个用户是否只输入了他/她的名字的字母。如果没有,请再次询问他们的姓名 System.out.print("Welcome - What is your family's surname? "); familySurname = keyboard.nextLine(); while (familySurname.isEmpty()) { System.out.print("Invalid name - What is your family's su

我是Java新手,我想知道如何检查一个用户是否只输入了他/她的名字的字母。如果没有,请再次询问他们的姓名

System.out.print("Welcome - What is your family's surname? ");
familySurname = keyboard.nextLine();
while (familySurname.isEmpty())  
{
    System.out.print("Invalid name - What is your family's surname? ");
    familySurname = keyboard.nextLine();
    if (familySurname.matches("[a-zA-Z]"))
    {
        System.out.println("Invalid Input.");
    }
}

这是我到目前为止的代码,但它仍然接受数字。

您的循环条件应该是:

 while (!familySurname.matches("[a-zA-Z]+")){
     System.out.print("Invalid name - What is your family's surname? ");
     familySurname = keyboard.nextLine();
 }

循环条件应为:

 while (!familySurname.matches("[a-zA-Z]+")){
     System.out.print("Invalid name - What is your family's surname? ");
     familySurname = keyboard.nextLine();
 }
还是为了速度-

public static boolean isAlpha(final String value) {

    if(value == null || value.isEmpty()){
        return false;
    }

    char[] chars = value.toCharArray();

    for (char c : chars) {
        if(!Character.isLetter(c)) {
            return false;
        }
    }

    return true;
}
您的代码片段可以修改为-

while (!isAlpha(familySurname)){

    System.out.print("Invalid name - What is your family's surname? ");
    familySurname = keyboard.nextLine();

}
还是为了速度-

public static boolean isAlpha(final String value) {

    if(value == null || value.isEmpty()){
        return false;
    }

    char[] chars = value.toCharArray();

    for (char c : chars) {
        if(!Character.isLetter(c)) {
            return false;
        }
    }

    return true;
}
您的代码片段可以修改为-

while (!isAlpha(familySurname)){

    System.out.print("Invalid name - What is your family's surname? ");
    familySurname = keyboard.nextLine();

}

你不想捕捉并修剪它吗

System.out.print("Welcome - What is your family's surname? ");
String familySurname = "";
while (familySurname.length() == 0) { // while we don't have a surname.
  if (keyboard.hasNextLine()) { // check that there is a line of input.
    familySurname = keyboard.nextLine().trim(); // get the line and trim() it.
    for (char c : familySurname.toCharArray()) {
      if (!Character.isLetter(c)) { // Test for not a letter.
        System.out.print("Invalid name - What is your family's surname? ");
        familySurname = "";
        break;
      }
    }
  }
}

你不想捕捉并修剪它吗

System.out.print("Welcome - What is your family's surname? ");
String familySurname = "";
while (familySurname.length() == 0) { // while we don't have a surname.
  if (keyboard.hasNextLine()) { // check that there is a line of input.
    familySurname = keyboard.nextLine().trim(); // get the line and trim() it.
    for (char c : familySurname.toCharArray()) {
      if (!Character.isLetter(c)) { // Test for not a letter.
        System.out.print("Invalid name - What is your family's surname? ");
        familySurname = "";
        break;
      }
    }
  }
}