So I am trying to make a JOptionPanel popup when a user doesn't put in a number from 0-60 in a JTextField, but I'm not sure how
Here is what I tried
else if(e.getSource() == Save) {
Task task = new Task();
task.hour = hin.getText();
task.min = min.getText();
if(min.getText() > 0 && min.getText() < 60) {
JOptionPane.showMessageDialog(null, "Please enter a valid time.");
}
else{
task.day = (String) dayspinner.getValue();
task.description = desc.getText();
tasks.add(task);
ObjectOutputStream os = null;
try{
os = new ObjectOutputStream(new FileOutputStream(save));
os.writeObject(tasks);
os.close();
}catch(IOException ex) {
ex.printStackTrace();
}
tframe.setVisible(false);
}
}
But I get errors that say "bad operand types for binary operator'<'".
getText()
method returns a String
, and you cannot use <
or >
to compare String
s. It would be better if you convert the result of min#getText
to an int
first, then do the proper comparison. You can do this by using Integer#parseInt()
:
int minVal = Integer.parseInt(min.getText());
//fixed the condition
if (!(minVal > 0 && minVal < 60)) {
//rest of the code...
}