[英]Why does this Java code throw a NumberFormatException?
I'm playing around with a GUI Sudoku solver that uses an array of JTextFields
( gridArray
) for display and an int
array ( sudokuGrid
) for the actual solving. 我正在玩一个GUI Sudoku解算器,该解算器使用JTextFields
数组( gridArray
)进行显示,并使用int
数组( sudokuGrid
)进行实际求解。 When I run it and it tries to cast the JTextField
string
s to int
s, it throws a NumberFormatException
on parsing the string
s into int
s, specifically this message: 当我运行它,它试图投的JTextField
string
s到int
S,它抛出一个NumberFormatException
的解析string
s转换int
S,特别是这条消息:
java.lang.NumberFormatException: For input string: ""
Here's the section of code that's causing me trouble: 这是引起我麻烦的代码部分:
// create solveButton
solveButton = new JButton("Solve It!");
solveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
// create grid and call Solve()
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
if (gridArray[i][j].getText() == "")
{sudokuGrid[i][j] = 0;}
else {sudokuGrid[i][j] = Integer.parseInt(gridArray[i][j].getText());}
}
} // end for loop
Solver(sudokuGrid);
// display solution
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
gridArray[i][j].setText(String.valueOf(sudokuGrid[i][j]));
}
} // end for loop
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(mainFrame,e.toString(),"Number Format Exception",JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(mainFrame,"Sorry, something broke, try again.","Solve Error",JOptionPane.ERROR_MESSAGE);
} // end try-catch
} // end actionPerformed()
}); // end solveButton ActionListener
I thought that the if
- else
would catch the empty fields and only try the parseInt
if there was a value present, but if anyone can enlighten me I'd appreciate it. 我认为if
- else
将捕获空字段,并且仅在存在值的情况下才尝试parseInt
,但是如果有人可以启发我,我将不胜感激。
You are checking string equality using ==
, which is only for reference equality. 您正在使用==
检查字符串是否相等,这仅用于引用相等。 Perhaps you meant to write: 也许您打算写:
gridArray[i][j].getText().equals("")
Your problem is here: 您的问题在这里:
if (gridArray[i][j].getText() == "")
You can't compare strings that way. 您不能那样比较字符串。 Do it this way instead: 改为这样:
if (gridArray[i][j].getText().equals(""))
Don't ask the TextArea for it's text, since this may be prone to be still in the editing process. 不要要求TextArea作为文本,因为它可能仍处于编辑过程中。 Check the underlying document itself. 检查基础文档本身。
Document document = gridArray[i][j].getDocument();
sudokuGrid[i][j] = document.getLength() == 0 ? 0 : Integer.parseInt(document.getText(0, 1);
Also... why a JTextArea? 另外...为什么要使用JTextArea? Why not a JTextField? 为什么不使用JTextField? You might even combine this with a JSpinner with values from 0 (which is inerpreted as empty to 9. 您甚至可以将其与JSpinner结合使用,该JSpinner的值从0(被解释为空到9)。
Using ==
-comparison with strings does not mean checking for equality of the text (string contents), but instead equality of the String-objects (testing are they the exact same OBJECT). 对字符串使用==
-comparison并不意味着检查文本(字符串内容)是否相等,而是检查字符串对象的相等性(测试它们是否具有完全相同的对象)。 Use String.equals() instead. 使用String.equals()代替。
The problem is your equality check: 问题是您的平等性检查:
gridArray[i][j].getText() == ""
This does not do what you're intending. 这不能满足您的预期。 In Java this checks whether the two strings are the same object not whether their values are equal. 在Java中,它检查两个字符串是否是同一对象,而不是它们的值是否相等。 You should use the String.equals() method to evaluate whether the text field is empty. 您应该使用String.equals()方法来评估文本字段是否为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.