【问题标题】:Scanner nextLine() issues扫描仪 nextLine() 问题
【发布时间】:2014-04-21 01:32:20
【问题描述】:

一段时间以来,我一直在从事一项充当拼字游戏词典的编程任务。该程序从用户那里获取输入并输出一个包含单词列表的文件,具体取决于用户从菜单中请求的内容。我遇到的问题与 Scanner.nextLine() 有关。

我不确定为什么,但出于某种原因,有时我必须按一次回车,然后我的代码才会接受我的输入并将其存储为变量。本质上,我最终输入了两次。我尝试在代码周围插入 Scanner.nextLine() 以“占用”空的输入/空格,但它不起作用,我必须多次按 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;
    }
}

最后,我必须按回车键一次,然后输入才能将任何内容存储在变量 firstLetter 中。我认为这与 nextLine() 的性质有关,因为使用 nextInt() 的条件没有问题。

【问题讨论】:

  • 如果您希望人们自愿花时间帮助您解决问题,那么您至少应该努力正确地格式化您的代码。
  • 谢谢,我很抱歉老兄,我按了八次,它仍然无法格式化。 Control+K 也做了插孔。不过万岁还是仇杀。
  • 制表符不是你的朋友 :) 下次尝试只使用空格。

标签: java java.util.scanner


【解决方案1】:

这是因为你同时使用了 nextLine() 和 nextInt(),所以 nextLine() 正在搜索新行(回车),如果通过 System.Integer 输入任何整数,nextInt 将自动停止搜索。在。

经验法则:只需使用 Scanner.nextLine() 作为您的输入,然后通过 Integer.parseInt(string) 等相应地从 Scanner.nextLine() 转换您的字符串。

【讨论】:

  • 感谢您的建议 - 我之前遇到过这个问题,这就是循环之间的 in.nextLine() 的用途。我将其更改为解析为 Int,但在程序识别它并将其分配给变量并关闭循环之前,我仍然需要输入两到三遍。
  • 它在您刚刚调用 in.nextLine() 的开头。您将按回车键但没有得到任何东西,因为您没有将字符串存储在.nextLine() 正在返回。
  • 谢谢!我会拿出来的。
【解决方案2】:

我认为你用太多的nextLines 过度补偿了。您可能希望这样做一次 以在输入 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() 之间立即添加一个额外的in.nextLine()

编辑:

另外,请注意,每当您调用 in.nextLine() 时,您都会吸收一行输入。例如,这条线应该是固定的:

        if(in.nextLine().length() > 1){

因为它读入一行,用完,然后检查那行(现在用完)是否足够长。

【讨论】:

  • 谢谢,我只是随便把它们放进去看看是否有帮助(我知道这技术很棒)。但是,一旦我把所有这些都取出来,程序仍然让我输入三遍,然后再将我的输入分配给变量并关闭循环。
  • 哎呀!!好的,所以我可能应该改变我的状况。不过,我也不知道我应该改变什么。
  • 也许你可以在前一行创建一个String s;,然后使用 (s=in.nextLine()).length() 从而将in.nextLine() 保存在s 中,然后你可以在以后的操作中使用s 访问该输入行。
  • 谢谢,我非常感谢您的所有建议,但我不确定我是否完全理解最后的评论。我尝试将s中的nextLine的长度存储为一个int,但它并没有做太多。你能进一步澄清你的意思吗? (对不起!)
  • 尝试用(s=in.nextLine()).length()>1 替换if 语句的内部,然后用s 替换所有以后使用的in.nextLine()。 (确保你事先定义了String s;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多