java - 如何使用扫描仪 hasNext() 循环遍历一行键盘文本并验证用户输入的整数和字符串
问题描述
我在尝试了解如何循环遍历用户将提供给 ex 的键盘输入文本行时遇到问题:
阿尼卡 14 丹 16
我想读取每个标记并分配给字符串名称、整数、年龄、字符串名称、整数年龄。以该顺序。然而,这很容易,如果用户输入 Anika Anika Anika Anika 13 13 13 Dan 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();
我知道如果我做 top 我得到了正确的顺序,但是我想忽略额外的输入,但是写一个语句表达式为什么它是错误的,然后继续搜索下一个 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);
}
我尝试在 ifs 中使用 do 而它没有用。我的逻辑在某处是错误的,我认为可能是 has.next() 方法。
任何帮助将不胜感激!!
解决方案
如果在等待 Integer 时输入是一个单词,它将抛出 InputMismatchException。nextInt()
首先将值读取为字符串,然后将其解析为整数,因此如果使用 nextInt 忽略该值,如果该值是一个单词,则会引发上述异常。
使用与程序相同的逻辑
更改应该是:
- 忽略输入
scan.next()
- 检查字符串是否可以是整数(使用
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();
}
注意scan.newLine()
catch 子句中的,这是必需的,因为 Scanner 使用最后一个输入的缓存,所以如果没有重新读取,则进入无限循环条件。
祝你好运!
推荐阅读
- python - 使用不同的按钮调用相同的函数
- html - 如何使用 ng-model 名称访问 ng-minlength 和 ng-maxlength
- java - 避免捕获泛型异常并使用泛型接口
- php - 从计算机附加文件时无法使用 wp_mail 接收任何文件
- sql-server - 数据仓库 GUID 到 Int PrimaryKeys
- android - 我无法在操作栏上获得图标
- node.js - npm start 在反应项目中不起作用
- hive - 如何获取 HIVE/PySpark 表中每一列的唯一值?
- ios - 如何监听通知授权状态的变化
- sql-server - 如何每秒更新一次 Azure 搜索索引?