java - java 扫描器仅在检测到整数时才继续
问题描述
我在java中使用扫描仪,并试图让程序仅在检测到用户选择的整数但我编写的代码没有提供该功能时才继续。这是我的代码:
import java.util.Scanner;
/**
*
* @author Ansel
*/
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
AddressBook ad1 = new AddressBook();
String firstName="";
String lastName="";
String key="";
String street="";
String city="";
String county="";
String postalCode="";
String mNumber="";
int choice=0;
do{
System.out.println("********************************************************************************");
System.out.println("Welcome to the Address book. Please pick from the options below.\n");
System.out.println("1.Add user \n2.Remove user \n3.Edit user \n4.List Contact \n5.Sort contacts \n6.Exit");
System.out.print("Please enter a choice: ");
int reloop = 0;
do {
try {
scan.nextLine();
choice = scan.nextInt();
reloop ++;
} catch (Exception e) {
System.out.println ("Please enter a number!");
}} while(reloop == 0);
if(choice==1){
//Add user
System.out.print("Please enter firstname: ");
firstName=scan.next();
System.out.print("Please enter lastname: ");
lastName=scan.next();
scan.nextLine();
System.out.print("Please enter street:");
street=scan.nextLine();
System.out.print("Please enter city: ");
city=scan.next();
System.out.print("Please enter county: ");
county=scan.next();
System.out.print("Please enter postal code: ");
postalCode=scan.next();
System.out.print("Please enter Mobile number: ");
mNumber=scan.next();
Address address = new Address(street,city,county,postalCode,mNumber);
key = lastName + " ".concat(firstName);
Person person = new Person(firstName,lastName,address);
ad1.addContact(key,person);
System.out.println("key: " + key);
}
else if(choice==2){
//Remove user
System.out.print("Please enter name of user to remove: ");
key=scan.nextLine();
System.out.println("name:" + key);
ad1.removeContact(key);
}
else if(choice==3){
//Edit user
}
else if(choice==4){
//List contact
System.out.println("Enter name of contact you wish to lookup: ");
key=scan.nextLine();
ad1.listContact(key);
}
else if(choice==5){
//Sort contacts
}
else{
System.out.println("Invalid choice entered, please try again");
}
}while(choice!=6);
}
}
无法正常运行的主要代码是:
int reloop = 0;
do {
try {
scan.nextLine();
choice = scan.nextInt();
reloop ++;
} catch (Exception e) {
System.out.println ("Please enter a number!");
}} while(reloop == 0);
运行时此代码要求输入一个数字。例如,如果您输入一个字母,它将显示一个空白行,直到您输入另一个字母,然后它会说请输入一个数字。我不明白为什么它没有说请在出现字母或除 int 以外的任何内容时立即输入数字
解决方案
只需使用Scanner.nextLine
and就Integer.parseInt
可以避免混淆。
Scanner scan = new Scanner(System.in);
int choice = 0;
System.out.print("Please enter a choice: ");
int reloop = 0;
do {
try {
String input = scan.nextLine(); // Scan the next line from System.in
choice = Integer.parseInt(input); // Try to parse it as an int
reloop++;
} catch (Exception e) {
System.out.println("Please enter a number!");
}
} while (reloop == 0);
您也可以使用nextLine
after everynextInt
来终止该行,但我更喜欢int
单独解析,如上所述。它更清晰,更详细。
推荐阅读
- flask - AttributeError:“NoneType”对象没有 Flask 上传的“文件名”属性
- python - 如何修复:“re.error:在 23457 位置没有重复内容”?
- python-3.x - 计算给定神经网络的失败次数?
- c# - 我们可以有一个包含默认值 + 绑定数据的内容吗?
- c# - 动态向表中添加行的错误
- python - 如何处理大型网页抓取项目中的多线程?
- asp.net-core - Razor 视图中的三元字符串比较不起作用
- php - Docker 无法连接到特定端口上的主机
- flutter - 如何在颤动中将滚动视图放在堆栈小部件中
- python - 如何通过回调函数将一个 Bokeh 小部件的输出用作另一个小部件的输入?