Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008, 05:17 AM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
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:

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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-04-2008, 05:20 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Have a look at the fifth line in your code - there is an error there, for one.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-04-2008, 05:23 AM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-04-2008, 05:25 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Please show what you changed.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-04-2008, 05:30 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
While you're at it, you need to understand this line:
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:
Quote:
...
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 02-04-2008 at 05:34 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-05-2008, 08:37 AM
Cnu Cnu is offline
Member
 
Join Date: Feb 2008
Posts: 13
Cnu is on a distinguished road
int n=kboard.nextInt();

Try this one...
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-06-2008, 06:16 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method in a Switch Statement cart1443 New To Java 6 03-14-2008 04:48 AM
Switch statement to display the name of the month Java Tip Java Tips 0 01-04-2008 10:32 AM
Help with gigantamous switch statement trill New To Java 2 08-06-2007 09:11 AM
Problem with a switch statement in Java baltimore New To Java 2 08-02-2007 05:43 AM
Statement or Prepared Statement ? paty Database 3 08-01-2007 05:45 PM


All times are GMT +3. The time now is 02:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org