简体   繁体   中英

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. 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:

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.

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. 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? Why not a JTextField? You might even combine this with a JSpinner with values from 0 (which is inerpreted as empty to 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). Use String.equals() instead.

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. You should use the String.equals() method to evaluate whether the text field is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM