【发布时间】: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 行?
【问题讨论】: