Results 1 to 10 of 10
  1. #1
    keith is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default 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 04:17 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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()

  3. #3
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    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 01:30 PM.

  4. #4
    Jnoobs is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    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
    Java Code:
    public static void main(String args[]) {
    instead of
    Java Code:
    public static void main(String args[]);
    use
    Java Code:
    System.out.println("blahhhhhhh")
    and place a ; at the end of break in Case 2 and you should be fine

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

    Default

    Quote Originally Posted by Jnoobs View Post
    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
    Java Code:
    public static void main(String args[]) {
    instead of
    Java Code:
    public static void main(String args[]);
    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!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Excuse me, but which bit of "It's a bracket thing...check your curly braces, especially after main() " is confusing?

    ...;)

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

    Default

    Quote Originally Posted by Tolls View Post
    Excuse me, but which bit of "It's a bracket thing...check your curly braces, especially after main() " is confusing?

    ...;)
    Oops.... <sheepish grin>

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Excuse me, but which bit of "It's a bracket thing...check your curly braces, especially after main() " is confusing?

    ...;)
    Too tinny; you should've used more woody words like "garlic" or "bicycle" or "matrass".

    kindest regards,

    Jos ;-)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    It's a garlic thing...check your bicycles, especially after the mattress().

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    It's a garlic thing...check your bicycles, especially after the mattress().
    Quite nice, nice and woody; like "woman's undies" ... nice and woody too ...

    kind regards,

    Jos ;-)

Similar Threads

  1. need help understanding part of code
    By mustachMan in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 08:11 PM
  2. newbish question
    By mac in forum New To Java
    Replies: 6
    Last Post: 03-27-2010, 09:17 PM
  3. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 10:46 PM
  4. Help with Switch string code
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:00 AM
  5. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:43 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
  •