gpt4 book ai didi

java - 如何使用scanner hasNext()循环一行键盘文本并验证用户输入的整数和字符串

转载 作者:行者123 更新时间:2023-12-02 09:21:13 25 4
gpt4 key购买 nike

我在尝试理解如何循环用户将给出的键盘输入文本行时遇到问题,例如:

阿尼卡 14 丹 16

我想读取每个标记并分配给字符串名称、整数、年龄、字符串名称、整数年龄。以该顺序。然而,这很容易,如果用户输入阿尼卡 阿尼卡 阿尼卡 阿尼卡 13 13 13 丹 16

然后我希望程序:

Anika, 
Integer needed got String,
Integer needed got String,
Integer needed got String,
13,
String needed got Integer,
String needed got Integer,
Dan,
16

所以第一个永远是一个字符串,它是一个单词编辑:“word”,第二个是一个int,第三个字符串是一个“word”,第四个是int。但是,我无法模拟这一点。

Scanner scan = new Scanner(System.in);
String name = null;
int age = 0;
String name2= null;
int age2= 0;
if(scan.hasNext() == true)
name = scan.next();
age = scan.nextInt();
name2= scan.next();
age2= scan.nextInt();

我知道如果我执行顶部操作,我会得到正确的顺序,但我想忽略额外的输入,但编写一个语句表达式为什么它是错误的,然后继续搜索下一个 int 或第三个字符串,然后等等。

 boolean isstring = false;
boolean isnumber = false;
do {
if (scan.hasNext())
{
name = scan.next();
isstring = true;
}
else {

System.out.println("Need String got Integer");
isstring = false;
scan.next();

}
} while (!isstring);


do {
if (scan.hasNextInt())
{
age = scan.nextInt();
isnumber=true;
}
else {

System.out.println("Need Integer got String");
isnumber=false;
scan.nextInt();

}
} while (!isnumber);



do {
if (scan.hasNext())
{
name2 = scan.next();
isstring = true;
}
else {

System.out.println("Need String got Integer");
isstring = false;
scan.next();

}
} while (!isstring);


do {
if (scan.hasNextInt())
{
age2 = scan.nextInt();
isnumber = true;
}
else {

System.out.println("Need Integer got String");
isnumber=false;
scan.nextInt();

}
} while (!isnumber);


}

我尝试将 do while 与 ifs 一起使用,但没有成功。我的逻辑在某个地方是错误的,我认为这可能是 has.next() 方法。

任何帮助将不胜感激!!

最佳答案

如果在等待Integer时输入的是单词,则会抛出InputMismatchException。 nextInt() 首先将值作为 String 读取,然后将其解析为 Integer,因此如果使用 nextInt 忽略该值,如果该值是单词,则会抛出上述异常。

使用与程序相同的逻辑

更改应该是:

  1. 使用scan.next()忽略输入
  2. 检查字符串是否可以是整数(使用 scan.hasNextInt()),而不是是否是字符串,因为任何整数都可以表示为字符串。
  boolean isstring = false;
boolean isnumber = false;

do {
if (!scan.hasNextInt()) {
isstring = true;
name = scan.next();
} else {
isstring = false;
System.out.println("Need String got Integer");
scan.next();
}
} while (!isstring);

do {
if (scan.hasNextInt()) {
isnumber = true;
age = scan.nextInt();
} else {
isnumber = false;
System.out.println("Need Integer got String");
scan.next();
}
} while (!isnumber);

do {
if (!scan.hasNextInt()) {
isstring = true;
name2 = scan.next();
} else {
isstring = false;
System.out.println("Need String got Integer");
scan.next();
}
} while (!isstring);

do {
if (scan.hasNextInt()) {
isnumber = true;
age2 = scan.nextInt();
} else {
isnumber = false;
System.out.println("Need Integer got String");
scan.next();
}
} while (!isnumber);

使用 try/catch 和一个循环

使用 try/catch 的简单解决方案如下

public static void main (String[]args)
{
String name = null;
String name2 = null;
Integer age = null;
Integer age2 = null;
Scanner scan = new Scanner(System.in);
while (scan.hasNext())
{
try
{
if (name == null)
{
System.out.println("Please provide name: ");
name = getNameOrFail(scan);
System.out.println("Name set: " + name);
}
if (age == null)
{
System.out.println("Please provide age: ");
age = getAgeOrFail(scan);
System.out.println("Age set: " + age);
}
if (name2 == null)
{
System.out.println("Please provide name2: ");
name2 = getNameOrFail(scan);
System.out.println("Name2 set: " + name2);
}
if (age2 == null)
{
System.out.println ("Please provide age2: ");
age2 = getAgeOrFail (scan);
System.out.println ("Age2 set: " + age2);
}
}
catch (Exception e)
{
System.out.println(e.getMessage ()); // Print the message put int Exception(message) constructor
scan.nextLine(); // Flush the Scanner cache
}
}
}

public static String getNameOrFail(Scanner scan) throws Exception
{
if (scan.hasNextInt())
throw new Exception("Need String got Integer");
return scan.next();
}

public static Integer getAgeOrFail(Scanner scan) throws Exception
{
if (!scan.hasNextInt())
throw new Exception("Need Integer got String");
return scan.nextInt();
}

注意catch子句中的scan.newLine(),这是必需的,因为扫描器使用最后一个输入的缓存,因此如果不重新读取,您将进入无限循环条件。

祝你好运!

关于java - 如何使用scanner hasNext()循环一行键盘文本并验证用户输入的整数和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680536/

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