Results 1 to 6 of 6
  1. #1
    Znote's Avatar
    Znote is offline Member
    Join Date
    Oct 2009
    Location
    Norway
    Posts
    21
    Rep Power
    0

    Default What is wrong with my tiny code?

    Hello, I have a case code, but I cant get it to work, I have no idea why. :C

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    import static java.lang.System.out;
    
    class switch {
    	
    	public static void main(String args[]) {
    		Scanner lolScanner = new Scanner(System.in);
    		Random lolRandom = new Random();
    		int randomNumber;
    		
    		out.print("Type your question, my child: ");
    		myScanner.nextLine();
    		
    		randomNumber = lolRandom.nextInt(10) + 1;
    		
    		switch (randomNumber) {
    			case 1:
    			out.println("Yes, isnt it obvious?");
    			break;
    			
    			case 2:
    			out.println("No, and don't ask me again.");
    			break;
    			
    			case 3:
    			out.print("Yessir, Yessir");
    			out.println("Three bags full.");
    			break;
    			
    			case 4:
    			out.print("What part of no ");
    			out.println("did you not understand?");
    			break;
    			
    			case 5:
    			out.println("No chance, Lance.");
    			break;
    			
    			case 6:
    			out.println("6");
    			break;
    			
    			case 7:
    			out.println("7");
    			break;
    			
    			case 8:
    			out.println("8");
    			break;
    			
    			case 9:
    			out.println("9");
    			break;
    					
    			case 10:
    			out.println("10 full score!");
    			break;
    			
    			default:
    			out.print("ERROR!");
    			break;
    		} 
    		out.println("Good bye!");
    	}
    	out.println("Script process stopped.");
    }
    Error message from Jcreator program:
    Java Code:
    --------------------Configuration: switch - JDK version 1.6.0_14 <Default> - <Default>--------------------
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:5: <identifier> expected
    class switch {
         ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
            ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                   ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                         ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ')' expected
            public static void main(String args[]) {
                                          ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                                               ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                                                ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                                                 ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                                                  ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:66: <identifier> expected
            out.println("Script process stopped.");
                       ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:66: illegal start of type
            out.println("Script process stopped.");
                        ^
    11 errors
    
    Process completed.

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

    Default

    You can't name a java class using a reserve word such as switch. Please change your class name to something like SwitchTest (note that class names should be capitalized).

    Next: you're using a variable "myScanner", but you never declare this variable nor initialize it.
    Next: your last out.println statement is being called outside of a method block, and this cannot be.

    It looks like you're writing your code without testing it enough. I suggest that you compile your code with each 1-2 lines of code added, and fix the errors before adding any other lines. Otherwise you risk having a program that has a bunch of errors.
    Last edited by Fubarable; 11-07-2009 at 04:17 PM.

  3. #3
    Znote's Avatar
    Znote is offline Member
    Join Date
    Oct 2009
    Location
    Norway
    Posts
    21
    Rep Power
    0

    Default

    Well, here is my result again:
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    import static java.lang.System.out;
    
    class switch {
    	
    	public static void main(String args[]) {
    		Scanner lolScanner = new Scanner(System.in);
    		Random lolRandom = new Random();
    		int randomNumber;
    		
    		out.print("Type your question, my child: ");
    		lolScanner.nextLine();
    		
    		randomNumber = lolRandom.nextInt(10) + 1;
    		
    		switch (randomNumber) {
    			case 1:
    			out.println("Yes, isnt it obvious?");
    			break;
    			
    			case 2:
    			out.println("No, and don't ask me again.");
    			break;
    			
    			case 3:
    			out.print("Yessir, Yessir");
    			out.println("Three bags full.");
    			break;
    			
    			case 4:
    			out.print("What part of no ");
    			out.println("did you not understand?");
    			break;
    			
    			case 5:
    			out.println("No chance, Lance.");
    			break;
    			
    			case 6:
    			out.println("6");
    			break;
    			
    			case 7:
    			out.println("7");
    			break;
    			
    			case 8:
    			out.println("8");
    			break;
    			
    			case 9:
    			out.println("9");
    			break;
    					
    			case 10:
    			out.println("10 full score!");
    			break;
    			
    			default:
    			out.print("ERROR!");
    			break;
    		} 
    		out.println("Good bye!");
    	}
    
    }
    It still does not work, here is the error report:
    Java Code:
    --------------------Configuration: switch - JDK version 1.6.0_14 <Default> - <Default>--------------------
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:5: <identifier> expected
    class switch {
         ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
            ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                   ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                         ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ')' expected
            public static void main(String args[]) {
                                          ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                                               ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                                                ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: illegal start of expression
            public static void main(String args[]) {
                                                 ^
    C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\switch\switch.java:7: ';' expected
            public static void main(String args[]) {
                                                  ^
    9 errors
    
    Process completed.
    About the switch: Well, what I really am doing, is reading a book, and I am trying to rewrite a code listed in the book using my eyes, and changing the values I know I can change without messing the script up. However I seem to be failing bigtime. :(

    Here is the original script, it works perfect. Note that on this script, it is still using the word switch, not any capitalized class word.
    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 your question, my child:  ");
            myScanner.nextLine();
    
            randomNumber = myRandom.nextInt(10) + 1;
    
            switch (randomNumber) {
            case 1:
                out.println("Yes. Isn't it obvious?");
                break;
    
            case 2:
                out.println("No, and don't ask again.");
                break;
    
            case 3:
                out.print("Yessir, yessir!");
                out.println(" Three bags full.");
                break;
    
            case 4:
                out.print("What part of 'no'");
                out.println(" don't you understand?");
                break;
    
            case 5:
                out.println("No chance, Lance.");
                break;
    
            case 6:
                out.println("Sure, whatever.");
                break;
    
            case 7:
                out.print("Yes, but only if");
                out.println(" you're nice to me.");
                break;
    
            case 8:
                out.println("Yes (as if I care).");
                break;
    
            case 9:
                out.print("No, not until");
                out.println(" Cromwell seizes Dover.");
                break;
    
            case 10:
                out.print("No, not until");
                out.println(" Nell squeezes Rover.");
                break;
    
            default:
                out.print("You think you have");
                out.print(" problems?");
                out.print(" My random number");
                out.println(" generator is broken!");
                break;
            }
    
            out.println("Goodbye");
        }
    }
    This leaves no errors. I cant see any difference between the scripts except that my is replaced with lol and some prints are different.

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

    Default

    Note that on this script, it is still using the word switch, not any capitalized class word.
    Please re-read that example code that you posted (the second class) as I think that you may be misunderstanding something very basic here. The class you've posted uses a class name that is capitalized and is not a reserve word,
    Java Code:
    class TheOldSwitcheroo { // the class name is TheOldSwitcheroo
    Again, the class name here is "TheOldSwitcheroo", which is very very different from trying to use "switch" as the class name. This one will work and class name switch won't.

    Please feel free to let me know if anything here doesn't make sense to you.

    Best of luck!
    Last edited by Fubarable; 11-07-2009 at 05:38 PM.

  5. #5
    Znote's Avatar
    Znote is offline Member
    Join Date
    Oct 2009
    Location
    Norway
    Posts
    21
    Rep Power
    0

    Default

    AJAJAJ STUPID STUPID ME ARRGHHH! <knocks myself to the ground>

    How didnt I manage to see that=?! XD

    It works perfect now, I didn't understand you quite, but thanks for showing me. :P

    That was the error, now it works perfectly. Arghh... I hate myself, been studying that script for over 2 hours, couldn't find anything, ANYTHING at all! And you saw it at the first sight of my script. x)

    Thank you so much! :D Thread solved. ^^

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

    Default

    No problem. And please don't beat yourself up too much. That's our job!

    Good luck!

Similar Threads

  1. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-20-2009, 12:59 AM
  2. pls tell wat wrong with my code???
    By low224 in forum New To Java
    Replies: 13
    Last Post: 01-11-2009, 08:40 AM
  3. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 06:44 PM
  4. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 02:50 AM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 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
  •