如何使用扫描器 hasNext() 遍历一行键盘文本并验证用户输入的整数和字符串
How to use scanner hasNext() to loop through a line of keyboard text and validate the user input of interger and string
我在尝试了解如何循环遍历用户将给出的键盘输入文本行时遇到问题,例如:
阿尼卡 14 丹 16
我想读取每个标记并分配给 String name、Int、age、String name、int age。以该顺序。
这很容易,但是,如果用户输入
阿妮卡 阿妮卡 阿妮卡 阿妮卡 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",第二个是一个整数,第三个字符串是一个"word",第四个是整数。
但是,我无法模拟这一点。
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() 方法。
任何帮助将不胜感激!!
如果在等待整数时输入的是单词,则会抛出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();
}
注意catch子句中的scan.newLine()
,这是必需的,因为Scanner使用最后一次输入的缓存,所以如果没有重新读取你进入无限循环条件。
祝你好运!
我在尝试了解如何循环遍历用户将给出的键盘输入文本行时遇到问题,例如:
阿尼卡 14 丹 16
我想读取每个标记并分配给 String name、Int、age、String name、int age。以该顺序。 这很容易,但是,如果用户输入 阿妮卡 阿妮卡 阿妮卡 阿妮卡 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",第二个是一个整数,第三个字符串是一个"word",第四个是整数。 但是,我无法模拟这一点。
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() 方法。
任何帮助将不胜感激!!
如果在等待整数时输入的是单词,则会抛出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();
}
注意catch子句中的scan.newLine()
,这是必需的,因为Scanner使用最后一次输入的缓存,所以如果没有重新读取你进入无限循环条件。
祝你好运!