Results 1 to 7 of 7
  1. #1
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default Switch Statement Help

    Alrighty, so I have to do this assignment for my Java course, using a switch statement. The assignment is to have the user enter a number (1-5), and have the corresponding line of a poem be displayed. So if the user entered 1, "One two, buckle your shoe" would be displayed. This is what I have and it's giving me a huge problem:

    Java Code:
    import java.util.Scanner;
    
    public class Poem
    {
    	public static void main(String[] args);
    		
    		Scanner kboard = new Scanner(System.in);
    		System.out.println("Enter a number 1-5 (or 0 to quit).");
    		int n = kboard.nextLine();
    		
    
    		switch (n) {
    			case 1: System.out.println("One two, buckle your shoe.");
    			break;
    			case 2: System.out.println("Three four, shut the door.");
    			break;
    			case 3: System.out.println("Five six, pick up sticks.");
    			break;
    			case 4: System.out.println("Seven eight, lay them straight.");
    			break;
    			case 5: System.out.println("Nine ten, a big fat hen.");
    			break;
    			default: System.out.println("Goodbye.");
    			break;
    			
    			
    		}
    	}			
    }
    This is giving me a HUGE string of errors. (Something like 45). I'm wracking my brain here trying to figure this out. Any help is greatly appreciated. Thanks!

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Have a look at the fifth line in your code - there is an error there, for one.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    Well now all I'm getting is this for errors:


    --------------------Configuration: <Default>--------------------
    C:\JavaPrograms\Poem.java:8: <identifier> expected
    System.out.println("Enter a number 1-5 (or 0 to quit): ");
    ^
    C:\JavaPrograms\Poem.java:8: illegal start of type
    System.out.println("Enter a number 1-5 (or 0 to quit): ");
    ^
    C:\JavaPrograms\Poem.java:12: illegal start of type
    switch (n) {
    ^
    C:\JavaPrograms\Poem.java:12: <identifier> expected
    switch (n) {
    ^
    C:\JavaPrograms\Poem.java:14: orphaned case
    case 1: System.out.println("One two, buckle your shoe.");
    ^
    C:\JavaPrograms\Poem.java:30: class, interface, or enum expected
    }

    Not sure what's wrong in the fifth line also.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Please show what you changed.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    While you're at it, you need to understand this line:
    Java Code:
    ...
            int n = kboard.nextLine();
    ...
    See the API for Scanner, you're attempting to use the method nextLine() which returns a String but you're really trying to return an int, based on the variable you're trying to set(int n). Do you see a method in the API that will return that int for you?


    EDIT:
    ...
    Not sure what's wrong in the fifth line also.
    How do we terminate statements.. ? We do this with the ; operator, However in your case you look like you are at the beginning of a block - therefore you need the { operator. Not unlike what you corrected in the switch(n); line. :)
    Last edited by CaptainMorgan; 02-04-2008 at 05:34 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    int n=kboard.nextInt();

    Try this one...

  7. #7
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Cnu View Post
    int n=kboard.nextInt();

    Try this one...
    CNU, that was meant for the OP to figure out. If they post back that they're having considerable difficulty then it's probably OK to post what you posted, but doing this too soon doesn't help them, especially giving the flat-out answer. Something to consider.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 04:48 AM
  2. Replies: 0
    Last Post: 01-04-2008, 10:32 AM
  3. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:11 AM
  4. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:43 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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