Java 扫描仪nextLine()问题
一段时间以来,我一直在做一项编程作业,它就像一本拼字词典。该程序接收用户的输入并输出一个包含单词列表的文件,具体取决于用户从菜单中请求的内容。我遇到的问题与Scanner.nextLine()有关 我不太清楚为什么,但出于某种原因,有时在代码接收输入并将其存储为变量之前,我必须按一下enter键。基本上,我最后输入了两次输入。我尝试在代码周围插入Scanner.nextLine(),以“占用”空的enter/spaces,但它不起作用,我必须多次按enter键才能让它处理我想要的内容 有人有什么建议吗?我将感谢所有的帮助 下面是一些代码:Java 扫描仪nextLine()问题,java,java.util.scanner,Java,Java.util.scanner,一段时间以来,我一直在做一项编程作业,它就像一本拼字词典。该程序接收用户的输入并输出一个包含单词列表的文件,具体取决于用户从菜单中请求的内容。我遇到的问题与Scanner.nextLine()有关 我不太清楚为什么,但出于某种原因,有时在代码接收输入并将其存储为变量之前,我必须按一下enter键。基本上,我最后输入了两次输入。我尝试在代码周围插入Scanner.nextLine(),以“占用”空的enter/spaces,但它不起作用,我必须多次按enter键才能让它处理我想要的内容 有人有什么
System.out.println("Enter the length of the word you are" + " searching for.");
int n = -1;
while(!(n >=0)) {
if(in.hasNextInt())
n = in.nextInt();
else {
System.out.println("You have not entered a valid number.
Please enter a real number this time.");
in.nextLine();
}
}
in.nextLine();
System.out.println("Enter the first letter of the words" + " you are searching for.");
String firstLetter = "";
while(!(firstLetter.length() == 1)) {
if(in.nextLine().length() > 1) {
System.out.println("You have not entered a valid letter.
Please press enter and enter only one real letter.");
}
else if(in.hasNextInt()) {
System.out.println("Do not enter a number. Please enter one real letter.");
}
else {
in.nextLine();
firstLetter = in.nextLine();
break;
}
}
最后,我必须按enter键一次,然后按input键,让它在变量firstLetter中存储任何内容。我假设这与nextLine()的性质有关,因为使用nextLine()的条件没有问题。这是因为您同时使用nextLine()和nextLine(),发生的事情是nextLine()正在搜索新行(enter),如果通过System.in键入任何整数,nextLine将自动停止搜索
经验法则:只需使用Scanner.nextLine()进行输入,然后将Scanner.nextLine()中的字符串相应地转换为Integer.parseInt(字符串)等。因为您同时使用nextLine()和nextLine(),所以nextLine()正在搜索新行(enter)如果通过System.in键入任何整数,nextInt将自动停止搜索
经验法则:只需使用Scanner.nextLine()进行输入,然后将Scanner.nextLine()中的字符串相应地转换为Integer.parseInt(字符串)等。因为您同时使用nextLine()和nextLine(),所以nextLine()正在搜索新行(enter)如果通过System.in键入任何整数,nextInt将自动停止搜索
经验法则:只需使用Scanner.nextLine()进行输入,然后将Scanner.nextLine()中的字符串相应地转换为Integer.parseInt(字符串)等。因为您同时使用nextLine()和nextLine(),所以nextLine()正在搜索新行(enter)如果通过System.in键入任何整数,nextInt将自动停止搜索
经验法则:只需使用Scanner.nextLine()进行输入,然后将Scanner.nextLine()中的字符串相应地通过Integer.parseInt(字符串)等进行转换。我认为您用太多的
nextLine
s进行了过度补偿。您可能希望在输入int后执行一次以清除该行,例如,清除换行符,但第二次仅吸收额外的一行输入:
System.out.println("You have not entered a valid number. Please enter a real number this time.");
in.nextLine();//first time
}
}
in.nextLine();//this second time is unnecessary.
在这里,重复使用也会发生同样的情况:
in.nextLine();
firstLetter = in.nextLine();
break;
在输入nextSOMETHINGELSE()
和另一个nextLine()
之间,您应该只在.nextLine()中添加一个额外的
编辑:
此外,请注意,无论何时在.nextLine()中调用,
,都会吸收一行输入。例如,这条线应该是固定的:
if(in.nextLine().length() > 1){
因为它在一行中读取,用完它,然后检查该行(现在用完了)是否足够长。我认为你用太多的下一行
进行了过度补偿。您可能希望在输入int后执行一次以清除该行,例如,清除换行符,但第二次仅吸收额外的一行输入:
System.out.println("You have not entered a valid number. Please enter a real number this time.");
in.nextLine();//first time
}
}
in.nextLine();//this second time is unnecessary.
在这里,重复使用也会发生同样的情况:
in.nextLine();
firstLetter = in.nextLine();
break;
在输入nextSOMETHINGELSE()
和另一个nextLine()
之间,您应该只在.nextLine()中添加一个额外的
编辑:
此外,请注意,无论何时在.nextLine()中调用,
,都会吸收一行输入。例如,这条线应该是固定的:
if(in.nextLine().length() > 1){
因为它在一行中读取,用完它,然后检查该行(现在用完了)是否足够长。我认为你用太多的下一行
进行了过度补偿。您可能希望在输入int后执行一次以清除该行,例如,清除换行符,但第二次仅吸收额外的一行输入:
System.out.println("You have not entered a valid number. Please enter a real number this time.");
in.nextLine();//first time
}
}
in.nextLine();//this second time is unnecessary.
在这里,重复使用也会发生同样的情况:
in.nextLine();
firstLetter = in.nextLine();
break;
在输入nextSOMETHINGELSE()
和另一个nextLine()
之间,您应该只在.nextLine()中添加一个额外的
编辑:
此外,请注意,无论何时在.nextLine()中调用,
,都会吸收一行输入。例如,这条线应该是固定的:
if(in.nextLine().length() > 1){
因为它在一行中读取,用完它,然后检查该行(现在用完了)是否足够长。我认为你用太多的下一行
进行了过度补偿。您可能希望在输入int后执行一次以清除该行,例如,清除换行符,但第二次仅吸收额外的一行输入:
System.out.println("You have not entered a valid number. Please enter a real number this time.");
in.nextLine();//first time
}
}
in.nextLine();//this second time is unnecessary.
在这里,重复使用也会发生同样的情况:
in.nextLine();
firstLetter = in.nextLine();
break;
在输入nextSOMETHINGELSE()
和另一个nextLine()
之间,您应该只在.nextLine()中添加一个额外的
编辑:
此外,请注意,无论何时在.nextLine()中调用,
,都会吸收一行输入。例如,这条线应该是固定的:
if(in.nextLine().length() > 1){
因为它在一行中读取,使用它,然后检查该行(现在已用光)是否足够长。谢谢,我只是随机把它们放进去看看是否有帮助(我知道这是一项很棒的技术)。一旦我把所有这些都取出来,程序仍然会让我输入三次,然后再将我的输入分配给变量并结束循环。哎哟!!好的,所以我应该改变我的状况。不知道我该做什么