Results 1 to 7 of 7
Thread: Switch Statement Help
- 02-04-2008, 04:17 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
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:
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!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; } } }
- 02-04-2008, 04:20 AM #2
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)
- 02-04-2008, 04:23 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
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.
- 02-04-2008, 04:25 AM #4
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)
- 02-04-2008, 04:30 AM #5
While you're at it, you need to understand this line:
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?Java Code:... int n = kboard.nextLine(); ...
EDIT:
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. :)...
Not sure what's wrong in the fifth line also.Last edited by CaptainMorgan; 02-04-2008 at 04: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)
- 02-05-2008, 07:37 AM #6
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
int n=kboard.nextInt();
Try this one...
- 02-06-2008, 05:16 AM #7
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
-
Method in a Switch Statement
By cart1443 in forum New To JavaReplies: 6Last Post: 03-14-2008, 03:48 AM -
Switch statement to display the name of the month
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:32 AM -
Help with gigantamous switch statement
By trill in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:11 AM -
Problem with a switch statement in Java
By baltimore in forum New To JavaReplies: 2Last Post: 08-02-2007, 04:43 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks