表示にJTextFields
( )の配列を使用し、実際の解法に( )の配列を使用するGUI数独ソルバーで遊んでいます。それを実行してsをsにキャストしようとすると、 sをsに解析するときに、具体的には次のメッセージがスローされます。gridArray
int
sudokuGrid
JTextField
string
int
NumberFormatException
string
int
java.lang.NumberFormatException: For input string: ""
これが私に問題を引き起こしているコードのセクションです:
// 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
if
-else
は空のフィールドをキャッチしparseInt
、値が存在する場合にのみ試してみると思いましたが、誰かが私を教えてくれるなら、それをいただければ幸いです。