gpt4 book ai didi

java - 如何在java中使用扫描仪处理奇怪的输入

转载 作者:行者123 更新时间:2023-12-01 19:33:38 24 4
gpt4 key购买 nike

我正在编写代码,其中我正在制作一个应用程序,通过询问用户关于他或她想要执行的操作(例如查询、存储、添加联系人等)的特定选择来存储联系人。

下面是我的代码的子部分,我实际上是在尝试在用户输入错误的选择时解决问题。我的意思是,如果用户输入非数字字符,它应该再次要求选择,

package com.vakhariaKathan;
import java.util.Scanner;

public class Main
{
private static Scanner scanner = new Scanner(System.in);
private static MobilePhone mobilePhone = new MobilePhone();
public static void main(String[] args)
{
int choice;
boolean quit = false;
System.out.println("Hey there! :),\nI am your Mobile Phone,📳");
printInstructions();
while(!quit)
{
System.out.print("Enter choice: ");
if(!scanner.hasNextInt())
{
System.out.println("You entered something bad 😣");
scanner.nextLine();
continue;
}
choice = scanner.nextInt();scanner.nextLine();
switch (choice)
{
case 0:
printInstructions();
break;
case 1:
mobilePhone.printContacts();
break;
case 2:
handleAdditionOfContact();
break;
case 3:
System.out.print("Enter contact name to be changed: ");
mobilePhone.modifyContact(scanner.nextLine());
break;
case 4:
System.out.println("Enter any name of the following you want to delete.");
mobilePhone.showContactNames();
mobilePhone.removeContact(scanner.nextLine());
break;
case 5:
quit = true;
break;
default:
System.out.println("Enter a valid choice!");
break;
}

}

}

private static void printInstructions()
{
System.out.println("Currently i can do following operations.");
System.out.println("0.Print instructions.");
System.out.println("1.Show list of contacts.");
System.out.println("2.Add Contact.");
System.out.println("3.Modify a contact.");
System.out.println("4.Delete Contact.");
System.out.println("5.Quit");
}

//to handle addition of contact
private static void handleAdditionOfContact()
{
String[] output = takeInput();
mobilePhone.addContact(output[0],output[1]);
}

//Method to take user input
private static String[] takeInput()
{
String name,contact;
System.out.print("Enter name of the Contact: ");
name = handleEmptyEntry("Name");
if(mobilePhone.doesExist(name))
{
System.out.println("That contact already exists.\nTry a different name");
name = handleEmptyEntry("Name");
}
System.out.print("Enter contact number for " + name + ": ");
contact = handleEmptyEntry("contact");
return new String[] {name,contact};
}
//function to handle empty entry
private static String handleEmptyEntry(String fieldName)
{
String in = scanner.nextLine();
while (in.isBlank())
{
System.out.printf("%s field cannot be empty ‼\nEnter again:",fieldName);
in = scanner.nextLine();

}
return in;
}

}

主要有两种场景:

1)如果用户在没有按回车键的情况下输入了错误的字符,它也可以正常工作。2)但是当用户输入新行然后输入错误的字符时,它会表现出一些奇怪的行为。以下是显示这两种情况的输出。

Hey there! :),
I am your Mobile Phone,📳
Currently i can do following operations.
0.Print instructions.
1.Show list of contacts.
2.Add Contact.
3.Modify a contact.
4.Delete Contact.
5.Quit
Enter choice: p
You entered something bad 😣
Enter choice:

i
You entered something bad 😣
Enter choice: You entered something bad 😣
Enter choice: You entered something bad 😣
Enter choice:

很高兴能得到详细的解释!

最佳答案

如果您编写了 scanner.next() 那么您就不会遇到此问题,但是当您编写 scanner.nextLine() 时您会遇到此行为,因为之前输入非数字值 您按两次 Enter 键,然后输入非数字值。所以你得到了 3 次你输入了错误的内容。两次用于您按下的输入,一次用于非数字值。发生这种情况是因为换行符保留在缓冲区中,当您输入非数字值时,它会读取所有这些值并打印 您输入了错误的内容

因此,如果您按 Enter 键 5 次,然后输入非数字值,您将得到 You Entered Something bad 6 times 打印结果。

关于java - 如何在java中使用扫描仪处理奇怪的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59241721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com