I'm creating a little Timer program that has 3 input fields (hours, minutes and seconds). For some reason, the OverTwentyFourException exception doesn't work for the hours input field. Here's parts of my code:
static JTextField userInputHrs;
static JTextField userInputMins;
static JTextField userInputSecs;
static int hrsChosen;
static int minsChosen;
static int secsChosen;
startButton.addActionListener(e -> {
switch(startButton.getIcon().toString()) {
case "Pause":
timer.TimerFunctionality.pause();
break;
case "Resume":
timer.TimerFunctionality.resume();
break;
default:
try {
//Calculate seconds from user input
hrsChosen = Integer.parseInt(userInputHrs.getText());
minsChosen = Integer.parseInt(userInputMins.getText());
secsChosen = Integer.parseInt(userInputSecs.getText());
secsRemaining = hrsChosen * 3600 + minsChosen * 60 + secsChosen;
if(hrsChosen < 0 || minsChosen < 0 || secsChosen < 0)
throw new NegativeException();
if(hrsChosen > 24)
throw new OverTwentyFourException();
//Getter for two thirds of userInput for color change
twoThirdsInput = 66.66 * secsRemaining / 100;
//Getter for one third of userInput for color change
oneThirdInput = 33.33 * secsRemaining / 100;
timer.TimerFunctionality.start();
}
catch(NegativeException ee) {
userInputHrs.setText("00");
userInputMins.setText("00");
userInputSecs.setText("00");
}
catch(OverTwentyFourException ee) {
userInputHrs.setText("00");
}
catch(NumberFormatException ee) {
userInputHrs.setText("00");
userInputMins.setText("00");
userInputSecs.setText("00");
JOptionPane.showMessageDialog(
Gui.this, "INPUT ERROR: Please use digits",
"Invalid Input",
JOptionPane.ERROR_MESSAGE
);
}
}
});
OverTwentyFourException class:
class OverTwentyFourException extends Exception {
OverTwentyFourException() {
Gui gui = new Gui();
JOptionPane.showMessageDialog(
gui,
"INPUT ERROR: The 'Hours' number needs to be lower than 24",
"Invalid Input - Hours",
JOptionPane.ERROR_MESSAGE
);
}
}
If I type '25' inside the hours field, I get the message dialog but the text is not set back to '00' as per my code, and the buttons stop working. I don't understand why as the minutes and seconds fields work perfectly and they're idendical. I don't see any difference between hrsChosen and minsChosen / secsChosen