【问题标题】:Scanner hasNext() doesn't match correctly inside if statements扫描仪 hasNext() 在 if 语句中不正确匹配
【发布时间】:2014-09-18 22:13:21
【问题描述】:

我通过一个while循环创建了以下代码。我不知道为什么将循环设置为查看 17 行而不是 15 行。

while (scan.hasNext())
                {           
                    selectedUserName.setText(scan.next());
                    if(scan.nextLine().toString() == "Male")
                    {
                        male = new JRadioButton("Male", true);
                    }
                    else if(scan.nextLine().toString() == "Female")
                    {
                        female = new JRadioButton("Female", true);
                    }
                    selectedGenderText.setText(scan.nextLine());
                    if(scan.next().toString() == "Warrior")
                    {
                        userClassBox.setSelectedItem(classes[0]);
                    }
                    else if(scan.next().toString() == "Barbarian")
                    {
                        userClassBox.setSelectedItem(classes[1]);
                    }
                    else if(scan.next().toString() == "Mage")
                    {
                        userClassBox.setSelectedItem(classes[2]);
                    }
                    else if(scan.next().toString() == "Thief") 
                    {
                        userClassBox.setSelectedItem(classes[3]);
                    }
                    selectedClassText.setText(scan.nextLine());
                    pointsText.setText(scan.nextLine());
                    pointsSlider.setValue(Integer.parseInt(scan.nextLine()));
                    intelligenceText.setText(scan.nextLine());
                    intelligenceSlider.setValue(Integer.parseInt(scan.nextLine()));
                    dexterityText.setText(scan.nextLine());
                    dexteritySlider.setValue(Integer.parseInt(scan.nextLine()));
                    strengthText.setText(scan.nextLine());
                    strengthSlider.setValue(Integer.parseInt(scan.nextLine()));
                    wisdomText.setText(scan.nextLine());
                    wisdomSlider.setValue(Integer.parseInt(scan.nextLine()));
                }

我的输入文件有15行匹配上面的代码,

Warrior
Male
Male
Warrior
Warrior
0
0
10
10
30
30
60
60
0
0

if/else 语句是否引用了两个新行?当我运行程序时,它卡在以下行-

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at CharacterCreation$OpenCharListener.actionPerformed(CharacterCreation.java:450)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

这引用了从 - 开始的最后两行

                wisdomText.setText(scan.nextLine());
                wisdomSlider.setValue(Integer.parseInt(scan.nextLine()));

为什么 .nextLine() 功能查看 17 行而不是 15 行?

【问题讨论】:

    标签: java.util.scanner next


    【解决方案1】:

    请注意,Scanner.nextLine() 使用来自扫描仪的数据。所以在这个例子中:

    if(scan.nextLine().toString() == "Male")
    {
        male = new JRadioButton("Male", true);
    }
    else if(scan.nextLine().toString() == "Female")
    {
        female = new JRadioButton("Female", true);
    }
    

    在第一个 if 语句中,您将使用来自扫描仪的数据,如果条件为假,您将在 else-if 语句中使用它后面的任何内容。

    您可能只想执行String input = scan.nextLine();,然后与存储的字符串进行比较。

    【讨论】:

    • 谢谢,我会研究实现 scan.next()。我可能不得不删除 if 和 else 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多