Results 1 to 9 of 9

Thread: Error

  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default Error

    Hey guys I'm getting this error. Could someone please tell me how to fix this and whether the program I wrote is correct.

    Level1.java:38: 'case', 'default' or '}' expected
    band = JOptionPane.showInputDialog("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");
    ^
    Level1.java:38: 'case', 'default' or '}' expected
    band = JOptionPane.showInputDialog("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");
    ^
    Level1.java:40: 'case', 'default' or '}' expected
    case 1: System.out.println("date = 10 Nov 2010,\n location = O2 Academy Brixton");
    ^
    Level1.java:41: 'case', 'default' or '}' expected
    break;
    ^
    Level1.java:42: 'case', 'default' or '}' expected
    case 2: System.out.println("date = 3 Dec 2010,\n location = Alexandra Palace");
    ^
    Level1.java:43: 'case', 'default' or '}' expected
    break;
    ^
    Level1.java:44: 'case', 'default' or '}' expected
    case 3: System.out.println("date = 11 Nov 2010,\n location = O2 Academy Brixton");
    ^
    Level1.java:45: 'case', 'default' or '}' expected
    break;
    ^
    Level1.java:46: 'case', 'default' or '}' expected
    default: System.out.println(" Error. Please type in the number of group listed.");
    ^
    Level1.java:51: ';' expected
    band1 = Vampire Weekend;
    ^
    Level1.java:52: ';' expected
    band2 = Of Montreal;
    ^
    Level1.java:53: ';' expected
    band3 = Someone Still Loves You Boris Yeltsin;
    ^
    12 errors
    the program I wrote:

    import javax.swing.*; // import the swing library for I/O

    class Level1
    {
    public static void main (String[] param)
    {

    popGroups();
    System.exit(0);

    } // END main


    public static void popGroups()
    {

    int band;
    band = 1;

    switch (band)
    {
    band = JOptionPane.showInputDialog("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");

    case 1: System.out.println("date = 10 Nov 2010,\n location = O2 Academy Brixton");
    break;
    case 2: System.out.println("date = 3 Dec 2010,\n location = Alexandra Palace");
    break;
    case 3: System.out.println("date = 11 Nov 2010,\n location = O2 Academy Brixton");
    break;
    default: System.out.println(" Error. Please type in the number of group listed.");
    }

    } // END popGroups


    } // END class Level1

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For one, your switch is wrong. You can't have that band = stuff inside the switch, but rather have
    Java Code:
    switch (blah...) {
      //... nothing else goes here
      case 1:
    ...
    Also, please don't use quote tags when you should be using code tags.

    Luck

  3. #3
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default How do I make keyboard input

    how do I make a keyboard input, such a question will pop out and the user has to type in the number 1/2/3?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can call the JOptionPane, just don't call it inside of the switch block. Try putting it somewhere else... where do you think would be a good place to start?

  5. #5
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I modified it to this

    import javax.swing.*; // import the swing library for I/O

    class Level1
    {
    public static void main (String[] param)
    {

    popGroups();
    System.exit(0);

    } // END main



    public static void popGroups()
    {

    int band;
    band = JOptionPane.showInputDialog("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");

    switch (band)
    {
    case 1: System.out.println("date = 10 Nov 2010,\n location = O2 Academy Brixton");
    break;
    case 2: System.out.println("date = 3 Dec 2010,\n location = Alexandra Palace");
    break;
    case 3: System.out.println("date = 11 Nov 2010,\n location = O2 Academy Brixton");
    break;
    default: System.out.println(" Error. Please type in the number of group listed.");
    break;

    }

    } // END popGroups


    } // END class Level1
    and I got this error

    Level1.java:35: incompatible types
    found : java.lang.String
    required: int
    band = JOptionPane.showInputDialog("Please type the number of group according the list below: \n 1. Vampire Weekend \n 2. Of Montreal \n 3. Someone Still Loves You Boris Yeltsin");
    ^
    1 error

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fubarable View Post
    Also, please don't use quote tags when you should be using code tags.
    Your code is all left justified and hard to read. Again, as I recommended above, please don't use quote tags where you should be using code tags. Please read the link in my signature below to see how to use code tags.

    Regarding your error, you'll want to read up on JOptionPane in the API (learning to inspect the API is a necessary skill for any Java programmer) to see what the showInputDialog returns. It isn't an int but rather a String, and since the band variable is expecting an int, you're getting the error shown. The Integer class will give you a method, parseInt(...) that will help you convert the one to the other.

    By the way, the error message,
    Level1.java:35: incompatible types
    found : java.lang.String
    required: int
    tells you exactly what's wrong: you're trying to put a String into an int variable.

    Luck!
    Last edited by Fubarable; 10-20-2010 at 12:19 AM.

  7. #7
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default Thanks A LOT.

    it's working perfectly now. thanks again.

  8. #8
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default One more question

    If let's say the user did not type in anything and just clicked ok
    how do I work on that case?
    and what if the user pressed esc, how do I modify my program so it doesnt come out error message?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by whateverme View Post
    If let's say the user did not type in anything and just clicked ok
    how do I work on that case?
    and what if the user pressed esc, how do I modify my program so it doesnt come out error message?
    Either read the API to see what that JOptionPane method returns when the user presses escape, or create and run a test program to see what is returned. Knowing this should show you how to deal with it.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 10:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 09:53 AM

Posting Permissions

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