JFormattedTextField和Integer对象之间的比较

时间:2012-11-02 03:09:55

标签: java swing integer jformattedtextfield

我有点奇怪的问题。

我正在写一个简单的数独程序。除了这种方法外,现在一切都很完美:

public static void checkAnswerKey()
    {
        int rowCounter = 0;
        int columnCounter = 0;

        System.out.println("The answers that are correct are (F is incorrect): ");

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                if (gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString())
                {
                    gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.correctAnswer);
                    System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + " ");
                }
                else
                {
                    gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.incorrectAnswer);
                    System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + io.puzzleAnswerArray[rowCounter][columnCounter].toString() + " ");
                    //System.out.print("F" + " ");
                }
            }
            System.out.println();
        }

这取自game.java文件。

现在,这个方法应该根据内部答案检查用户输入。它做的。但一切都变得不正确。

以下是io.java和gui.java文件的声明/实例化。

public class gui extends JFrame implements ActionListener{

...

    public static JFormattedTextField[][] userInputFormattedArray = new JFormattedTextField[9][9];
    //Create the container.
    public Container pane = getContentPane();
    //The font the game uses.
    public Font gameFont = new Font("Arial", Font.PLAIN, 30);
    //Correct and incorrect answer colors.
    static Color correctAnswer = new Color(100, 255, 100);
    static Color incorrectAnswer = new Color(255, 100, 100);

这两个方法在构造函数中启动:

public void showTextFields()
    {
        int rowCounter = 0;
        int columnCounter = 0;

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                userInputFormattedArray[rowCounter][columnCounter] = new JFormattedTextField();
                pane.add(userInputFormattedArray[rowCounter][columnCounter]);
                userInputFormattedArray[rowCounter][columnCounter].setFont(gameFont);
                userInputFormattedArray[rowCounter][columnCounter].setHorizontalAlignment(JTextField.CENTER);
            }
        }
    }

    public void setTextFields()
    {
        String heldString;
        boolean notEditable = false;
        int rowCounter = 0;
        int columnCounter = 0;

        for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
        {
            for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
            {
                if (game.puzzleUserShownArray[rowCounter][columnCounter] != 0)
                {
                    heldString = game.puzzleUserShownArray[rowCounter][columnCounter].toString();
                    userInputFormattedArray[rowCounter][columnCounter].setText(heldString);
                    userInputFormattedArray[rowCounter][columnCounter].setEditable(notEditable);
                }
            }
        }
    }

这就是答案的来源:

public static Integer[][] puzzleAnswerArray = new Integer[9][9];

public void getPuzzle() throws FileNotFoundException
    {
        Scanner puzzlesInFile = new Scanner(new FileReader("C:\\Users\\owner\\Desktop\\eclipse\\projects\\Sudoku\\Sudoku\\src\\puzzles.dat"));
        String searchParameter = "#puzzle" + intPuzzleSeed;
        int rowCounter = 0;
        int columnCounter = 0;

        //Prints the search parameter into the console, ensuring accuracy with the RNG.
        System.out.print(searchParameter);
        System.out.println();

        //
        puzzlesInFile.findWithinHorizon(searchParameter, 0);
        while (puzzlesInFile.hasNextInt())
        {
            for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
            {
                for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
                {
                    puzzleAnswerArray[rowCounter][columnCounter] = puzzlesInFile.nextInt();
                    System.out.print(puzzleAnswerArray[rowCounter][columnCounter].toString() + " ");
                }
                System.out.println();
            }
        }
        puzzlesInFile.close();
    }

我希望这是足够的信息。我错过了一些明显的东西吗顺便提一下,这是完成拼图的控制台输出:

正确的答案是(F不正确): 11 22 33 44 55 66 77 88 99 44 55 66 77 88 99 11 22 33 77 88 99 11 22 33 44 55 66 22 33 44 55 66 77 88 99 11 55 66 77 88 99 11 22 33 44 88 99 11 22 33 44 55 66 77 33 44 55 66 77 88 99 11 22 66 77 88 99 11 22 33 44 55 99 11 22 33 44 55 66 77 88

这是GUI:

---编辑:我不能发布GUI,因为我的声誉不到10。一到那里,我就会发布一个.png的GUI .---

我的猜测是即使“字符串”是相同的,但由于某种原因,它不会将其视为相同。如果您需要查看更多代码,请告诉我,但我认为我已涵盖所有内容。

1 个答案:

答案 0 :(得分:1)

您正在使用==进行字符串相等性检查。

if(gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString()) {

请改用.equals()方法。这不仅适用于字符串,也适用于任何类型的对象。如果对象==检查对象的身份。

尝试:

if(gui.userInputFormattedArray[rowCounter][columnCounter].getText().equals(io.puzzleAnswerArray[rowCounter][columnCounter].toString())) {