Help with this java code? help appreciated
Hi there am fairly new to learning java and I would like some help on this code, thanks.
import javax.swing.JOptionPane;
public class time
{
public static void main(String[] args)
{
String time = JOptionPane.showInputDialog ("insert the current time to the nearest hour in 24 hour format");
int realtime = Integer.parseInt(time);
switch (realtime)
{
case "<12":
JOptionPane.showMessageDialog (null,"good morning");
break;
case "> 12 && < 17":
JOptionPane.showMessageDialog (null,"good afternoon");
break;
default:
JOptionPane.showMessageDialog (null,"good evening");
break;
}
JOptionPane.showMessageDialog (null,"Thank you for using the special clock");
System.exit(0);
}
}
The errors
line 11 - incompatible types
line 15 - incompatible types
Re: Help with this java code? help appreciated
Use the code tag please,
anyway your cases are strings "<12" is a string and you equal it to an integer (realtime).. remove the quotes.
and it also false to ask that way .. you have to do:
case 1:
case 2:
case 3:
case 4:
~~
~~
~~
case 12: code.
The code will be for 1-12.
Depends on your wills .. you can also do it with default(if none of the cases did happens then the default will).
Anyway read about it: The switch Statement
Re: Help with this java code? help appreciated
Might also be worth enclosing the parseInt in a try/catch block? Just in case someone doesn't enter a valid number?
~Longeye~