Results 1 to 3 of 3
  1. #1
    jordan123 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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
    Last edited by tnrh1; 11-06-2011 at 02:30 PM.

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default 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~

Similar Threads

  1. Replies: 3
    Last Post: 11-05-2011, 03:52 PM
  2. Java need help! Help will be very much appreciated
    By paccerz in forum New To Java
    Replies: 1
    Last Post: 08-23-2011, 08:47 PM
  3. Replies: 10
    Last Post: 06-05-2011, 08:16 PM
  4. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 09:30 PM
  5. Help with newb-project much appreciated
    By barr in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 11:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •