【发布时间】:2015-12-08 20:14:32
【问题描述】:
我是 java 新手,我想知道是否可以使用其他方法代替 has.nextInt(),因为这种方法会弄乱我的扫描仪。例如
do {
System.out.println("Please enter your full name: ");
memberName = input.nextLine();
if (input.hasNextInt()) {
System.out.println("Your name cannot contain a number");
input.next();
} else {
successful = true;
}
} while (successful == false);
控制台
Create new member
Please enter your full name:
jack
jack
我必须输入我的名字两次才能继续 我知道有这样的问题,但我对任何解决方案都没有运气。谢谢
编辑
我正在尝试确保输入根本不包含任何数字,如果包含则
System.out.print("Your name cannot contain any numbers");
发生
【问题讨论】:
-
你希望
input.hasNextInt()做什么? -
你能提供一些你想接受的名字的例子吗?喜欢你要接受
foo123吗?或者如果有人有两个名字,比如Jack Adam Smith? -
我只想接受像乔博客,乔恩史蒂文斯这样的信件。我不想接受任何数字或 foo123 @Pshemo
-
也许这篇 SO 帖子会回答你的问题:Check if String contains only letters