Search code examples
javajava.util.scanner

Scanner hasNext() doesn't match correctly inside if statements


I have created the following code through making a while loop. I cannot tell why the loop is set to look at 17 lines and not 15 lines.

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()));
                }

My input file has 15 lines to match the code above,

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

Do the if/else statements reference two new lines? When I run the program it gets stuck on the following line-

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)

This reference the last two lines starting at -

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

Why does the .nextLine() functionality look at 17 lines instead of 15?


Solution

  • Note that Scanner.nextLine() consumes data from the scanner. So in this example:

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

    on the first if statement, you'll consume data from the scanner, and if the conditional is false, you'll then consume whatever follows it within the else-if statement.

    You probably just want to do String input = scan.nextLine(); and then compare against that stored string.