0

Having some issues getting this input validation to work.

What am I doing wrong?

When you select OK in the JOptionPane it still assigns the values and does not show the error dialog box.

public void actionPerformed(ActionEvent e) {
    do {
        int option = JOptionPane.showConfirmDialog(calendarPanel.getMainView(), message, "Enter OT Details",
                JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.CANCEL_OPTION) {
            break;
        }
        if (option == JOptionPane.OK_OPTION) {
            if (workTypeA.getText() != "" && startTimeA.getText() != "" && finishTimeA.getText() != ""
                    && otHoursA.getText() != "") {
                workTypeString = String.valueOf(workTypeA.getText());
                startTimeString = String.valueOf(startTimeA.getText());
                finishTimeString = String.valueOf(finishTimeA.getText());
                otHoursString = String.valueOf(otHoursA.getText());
                rateCodeString = String.valueOf(rateCodeA.getSelectedItem());
                System.out.println(workTypeString);
                valid = 1;
            } else {
                JOptionPane.showMessageDialog(null, "Please fill out all fields!", "Error",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    } while (valid == 0);
}
Laurel
  • 5,965
  • 14
  • 31
  • 57

1 Answers1

0

You do not assign valid value to 0 nowhere in your code. That means, that after one correct input everything will be valid. As a sidenote - java has boolean type that you should use instead when dealing with true-false values.

Additionally, do not use != or == in String comparisons. That compares references, and in most situations they will not be the same even for the same text content - see How do I compare strings in Java?

Community
  • 1
  • 1
Deltharis
  • 2,320
  • 1
  • 18
  • 29
  • Thanks. I have declared valid as 0 in the beginning and didn't show it in my code above. Sorry about that. I forgot about .equals() as I have been studying other languages and only just came back to Java. Thanks for the help! :) – user3126322 Sep 01 '14 at 12:04