0

I am trying to apply 2 conditions: read a character at a time and require alpha character input for the same scanner input:

Any suggestions on how to apply these conditions and continue checking each character user inputs?

Scanner lastname = new Scanner(System.in);
System.out.println("Please enter the first letter of last name:");
lastname.useDelimiter("(?<=.)");  
if (lastname.hasNext())  
    System.out.println("character: [" + lastname.next() + "]");
if (!lastname.hasNext("[A-Za-z]+")); {
    System.out.println("You are not a robot so do not use numeric characters.");
  System.out.println("Please enter letter:");
    lastname.next();
4

1 回答 1

0

不要更改分隔符;改变你的逻辑。

始终只读取一个字符,但使用如下代码对其进行测试:

String s = scanner.next();
if (Character.isDigit(s.charAt(0)))

或使用正则表达式:

if (s.matches("[a-zA-Z]+"))
于 2013-07-09T01:43:31.987 回答