Results 1 to 10 of 10
- 09-21-2010, 11:44 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 1
- Rep Power
- 0
Can anyone see the problem with my code? problem understanding switch (newbish)
//mostly taken from Java programming for dummies
// below I pasted the errors I get
Java Code:import java.util.Scanner; import java.util.Random; import static java.lang.System.out; class TheOldSwitcheroo { public static void main(String args[]); Scanner myScanner=new Scanner(System.in); Random myRandom=new Random(); int randomNumber; out.print("Type you question..."); myScanner.nextLine(); randomNumber=myRandom.nextInt(4)+1; switch (randomNumber) { case 1: out.print("Yes, isnt it obvious"); break; case 2: out.print("No way!"); break; case 3: out.print("Anything is possible"); break case 4: out.print("I doubt it"); break; default: out.print("you think you have problems"); out.print("the random number generator "); out.print("in this program is broken"); break ; } out.println("Good Bye"); } }
Java Code:-------------------------------------------------- ----jGRASP exec: javac -g TheOldSwitcheroo.java TheOldSwitcheroo.java:13: <identifier> expected out.print("Type you question..."); ^ TheOldSwitcheroo.java:13: illegal start of type out.print("Type you question..."); ^ TheOldSwitcheroo.java:14: <identifier> expected myScanner.nextLine(); ^ TheOldSwitcheroo.java:16: <identifier> expected randomNumber=myRandom.nextInt(4)+1; ^ TheOldSwitcheroo.java:18: illegal start of type switch (randomNumber) { ^ TheOldSwitcheroo.java:18: <identifier> expected switch (randomNumber) { ^ TheOldSwitcheroo.java:20: orphaned case case 1: ^ TheOldSwitcheroo.java:30: ';' expected break ^ TheOldSwitcheroo.java:42: <identifier> expected out.println("Good Bye"); ^ TheOldSwitcheroo.java:42: illegal start of type out.println("Good Bye"); ^ TheOldSwitcheroo.java:45: class, interface, or enum expected } ^ 11 errors ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.Last edited by Fubarable; 09-21-2010 at 03:17 PM. Reason: Moderator Edit: Code tags added
- 09-21-2010, 12:13 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
One of the problems is you haven't posted your code using CODE tags, so the formatting has gone, making it harder to read.
You haven't defined "out".
ETA: Gah!
Missed the static import!
:)
It's a bracket thing...check your curly braces, especially after main()
- 09-21-2010, 12:14 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
First of all, i havent seen out.println... out is a variable class of System, so you have to put
System.out.println();
Try this and tell us...
or as tolis said...
you can define out as an instance and a method inside called print() but as you are new i suppose that all you need is System.out.print();
ETA: Gah!
Missed the static import! ME2!!!!!!!!!!!!!!!!! :d Tolis you are right (once again ;) ) !Last edited by jlmp; 09-21-2010 at 12:30 PM.
- 09-21-2010, 01:52 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
anytime you are using a main (or any other type) of method and placing things inside, you must use { and } to open and close the function. so do
instead ofJava Code:public static void main(String args[]) {useJava Code:public static void main(String args[]);
and place a ; at the end of break in Case 2 and you should be fineJava Code:System.out.println("blahhhhhhh")
-
Jnoobs: Sharp eyes -- nice catch!
To the original poster, I've added code tags to your post. To learn how to do this yourself in future posts, read the top link in my signature below. Also a key technique when creating Java programs is to compile often and to always fix compilation problems before adding new code.
Much luck!
- 09-21-2010, 03:24 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Excuse me, but which bit of "It's a bracket thing...check your curly braces, especially after main() " is confusing?
...;)
-
- 09-21-2010, 03:52 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
- 09-21-2010, 03:59 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
It's a garlic thing...check your bicycles, especially after the mattress().
- 09-21-2010, 04:15 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
need help understanding part of code
By mustachMan in forum New To JavaReplies: 7Last Post: 02-09-2011, 07:11 PM -
newbish question
By mac in forum New To JavaReplies: 6Last Post: 03-27-2010, 08:17 PM -
trouble understanding code help
By yasmin k in forum New To JavaReplies: 4Last Post: 11-16-2009, 09:46 PM -
Help with Switch string code
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:00 AM -
Problem with a switch statement in Java
By baltimore in forum New To JavaReplies: 2Last Post: 08-02-2007, 04:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks