Results 1 to 13 of 13
- 10-18-2012, 08:07 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Compile errors: illegal start of expression, not a statement, ';' expected
Hello -
I've been studying other concepts in Java, but would like to go back to a little guess-the-number game I did late last month (see Do-while problem, or other problem?). Specifically, I am trying to add exception handling (although at this point I haven't added code to account for input out of range, which I'll do next).
I'm getting a "105: error: illegal start of expression" (line 105), plus a couple of "expected" (semicolon and perentheses) errors, and again at line 113. Both lines are the beginning of two methods outside of main.
Could someone please take a look and provide a hint? Are these methods out of place? Am I doing something else, like referencing variables outside of scope or something similar that causses the errors? The original code is the last post of the link above.
The current code follows:
Thanks for your help!
Java Code:import javax.swing.*; // GUI import java.util.Random; // Random public class GTNEH // class Guess The Number - Exception Handling { public static void main ( String[] args ) // main method { // declare and initialize variables int compNum, yourNum, tries; String selectYN, openingMessage, instructionMessage, attemptMessage, yournumString, winddowMessage, closingMessage; // display opening message openingMessage = "Find and defuse the bomb hidden behind a door!\nYou have 3 attempts to guess which behind which door the bomb hides."; JOptionPane.ShowMessageDialog ( null, openingMessage ); selectYN = "y"; // game routine do { // give instructions, randomize, reset tries to 0 instructionMessage = "Guess a door number from 1 to 10."; JOptionPane.ShowMessageDialog ( null, instructionMessage ); Random rand = new Random(); compNum = rand.nextInt(10)+1; tries = 0; boolean thereIsAnError = true; // new from chapter 8 - exception handling attemptMessage = "Attempt number " + tries + ":"; while ( tries < 3 ) { tries ++; // add 1 to tries counter while (thereIsAnError) { try { yourNum = getYourNum(attemptMessage); // process and output thereIsAnError = processYourNum(yourNum); } // end try catch ( NullPointerException e ) // for clicking Cancel or Close { String closingMessage = "Thanks for playing. Goodbye!"; JOptionPane.showMessageDialog( null, closingMessage ); System.exit(0); } // end catch catch ( NumberFormatException e ) // for entering nondigits { String errorMessage = "Please enter only digits."; JOptionPane.showMessageDialog( null, errorMessage ); tries = tries - 1; } // end catch finally { // do nothing } // end finally } // end while thereIsAnError //determine if numbers match if (yourNum == compNum) { //congratulations, play again? windowMessage = "Conrgatulations!!! \n"; tries = 0; JOptionPane.showMessageDialog( null, windowMessage ); PlayAgain(); } else { if ((yourNum < compNum) && (tries < 3)) { windowMessage = "Uh oh. The secret number is higher. \n"; JOptionPane.showMessageDialog( null, windowMessage ); } else { if ((yourNum > compNum) && (tries < 3)) { windowMessage = "Uh oh. The secret number is lower. \n"; JOptionPane.showMessageDialog( null, windowMessage ); } } } if ((tries ==3) && (yourNum != compNum)) { windowMessage = "KABOOOM!!! The secret number was " + compnum + ". \n\n"; JOptionPane.showMessageDialog( null, windowMessage ); PlayAgain(); } System.exit(0); } //end main public static int getYourNum( String attemptMessage ) { attemptMessage = "Attempt number " + tries + ":"; String yourNumString = JOptionPane.showInputDialog( attemptMessage ); int yourNum = Integer.parseInt( yourNumString ); //convert string to number return yourNum; } // end method getYourNum public static void PlayAgain() { String ynNoSelected = "n"; String selectYN, selectynMessage; selectynMessage = "Would you like to play again (y/n)?"; selectyn = JOptionPane.showInputDialog( selectynMessage ); if (selectyn.equals(ynNoSelected)) { System.exit(0); } } // end method PlayAgain } // end class }Last edited by pakupakuman; 10-18-2012 at 08:08 PM. Reason: typos
- 10-18-2012, 08:21 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Wow, there are so many coding errors...I dont know where to begin.. :)
I'll give you a hint: if you are beginner and still unsure, program one or two-line code, compile and see if everything is correct. If not, try to fix the code, only if anything is fine, continue programming
- 10-18-2012, 08:22 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Compile errors: illegal start of expression, not a statement, ';' expected
What's that stray } doing at the end?Java Code:} // end method PlayAgain } // end class }
Check your braces: make the placement of the opening brace consistent, and have the closing brace line up with its opening partner. The compiler messages are typical of what you get when you start the declaration of one method within another.
- 10-18-2012, 08:26 PM #4
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Why are you trying to do from the lines 101 to 107 ?
WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-18-2012, 08:37 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
- 10-18-2012, 08:41 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Lines 101 - 107 take your input (guess) of a number from 1 to 10.
Btw - thanks to all for replying and fotgive my brevity as I am curretly typing on the phone.
- 10-18-2012, 09:07 PM #7
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Ok so there are some capital letters errors.... also if you want to use variables in other methods you should make them public static
Also there is a variable that not variabled xD in the line 41 "processYourNum"
same for the "windowMessage" you misspelled it on the line 12Last edited by Lionlev; 10-18-2012 at 09:10 PM.
WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-18-2012, 09:19 PM #8
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Ok so, I've fixed some of the errors, but you have to do something with "processYourNum" because I can't find that it's declared somehow...
Java Code:import javax.swing.*; // GUI import java.util.Random; // Random public class GTNEH // class Guess The Number - Exception Handling { public static int compNum, yourNum, tries; public static String selectYN, openingMessage, instructionMessage, attemptMessage, yournumString, windowMessage, closingMessage; public static void main ( String[] args ) // main method { // declare and initialize variables // display opening message openingMessage = "Find and defuse the bomb hidden behind a door!\nYou have 3 attempts to guess which behind which door the bomb hides."; JOptionPane.showMessageDialog ( null, openingMessage ); selectYN = "y"; // game routine // give instructions, randomize, reset tries to 0 instructionMessage = "Guess a door number from 1 to 10."; JOptionPane.showMessageDialog ( null, instructionMessage ); Random rand = new Random(); compNum = rand.nextInt(10)+1; tries = 0; boolean thereIsAnError = true; // new from chapter 8 - exception handling attemptMessage = "Attempt number " + tries + ":"; while ( tries < 3 ) { tries ++; // add 1 to tries counter while (thereIsAnError) { try { yourNum = getYourNum(attemptMessage); // process and output thereIsAnError = processYourNum(yourNum); } // end try catch ( NullPointerException e ) // for clicking Cancel or Close { String closingMessage = "Thanks for playing. Goodbye!"; JOptionPane.showMessageDialog( null, closingMessage ); System.exit(0); } // end catch catch ( NumberFormatException e ) // for entering nondigits { String errorMessage = "Please enter only digits."; JOptionPane.showMessageDialog( null, errorMessage ); tries = tries - 1; } // end catch finally { // do nothing } // end finally } // end while thereIsAnError //determine if numbers match if (yourNum == compNum) { //congratulations, play again? windowMessage = "Conrgatulations!!! \n"; tries = 0; JOptionPane.showMessageDialog( null, windowMessage ); PlayAgain(); } else { if ((yourNum < compNum) && (tries < 3)) { windowMessage = "Uh oh. The secret number is higher. \n"; JOptionPane.showMessageDialog( null, windowMessage ); } else { if ((yourNum > compNum) && (tries < 3)) { windowMessage = "Uh oh. The secret number is lower. \n"; JOptionPane.showMessageDialog( null, windowMessage ); } } } if ((tries ==3) && (yourNum != compNum)) { windowMessage = "KABOOOM!!! The secret number was " + compNum + ". \n\n"; JOptionPane.showMessageDialog( null, windowMessage ); PlayAgain(); } } } //end main public static int getYourNum( String attemptMessage ) { attemptMessage = "Attempt number " + tries + ":"; String yourNumString = JOptionPane.showInputDialog( attemptMessage ); int yourNum = Integer.parseInt( yourNumString ); //convert string to number return yourNum; } // end method getYourNum public static void PlayAgain() { String ynNoSelected = "n"; String selectYN, selectynMessage; selectynMessage = "Would you like to play again (y/n)?"; selectYN = JOptionPane.showInputDialog( selectynMessage ); if (selectYN.equals(ynNoSelected)) { System.exit(0); } } // end method PlayAgain } // end classWARNING I am Russian so it's possible that I wont understand you correctly...
- 10-19-2012, 09:53 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Please don't do other people's work for them.
This isn't a coding service.
For the OP.
Step 1 is to sort out your indenting and formatting.
Match up all your brackets properly.
That'll show any problems in that area. At the moment it's a mess. Either use all tabs or all spaces, do not mix and match tabs with spaces for indenting.
Step 2 is to break up that massive code block in your main() method.
Huge blocks of code like that are a pain to debug.
I can identify 4 or 5 nice small methods in there.Please do not ask for code as refusal often offends.
- 10-19-2012, 08:06 PM #10
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Thanks! I compared your code to mine. I see that you've moved the variable initialization and declaration out of main, I presume so they can be referenced in the other methods? Why did you insert "public static" before them on lines 6 and 7?
Concerning processYourNum, that's a major blunder of mine. That is (will be) the method where your number and the computer's number is compared (lines 64-97). Funny that I didn't mention it in the post above; I may have missed during compilation. I'll fix that and the formatting. Regards.
- 10-20-2012, 01:02 AM #11
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Well if there is a variable out side of the methods or constructors, you should use "private" if you wont use it in other classes or "public" if you want to use it in other classes
:DWARNING I am Russian so it's possible that I wont understand you correctly...
- 10-20-2012, 10:34 AM #12
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Re: Compile errors: illegal start of expression, not a statement, ';' expected
Thanks again. It's compiling and running, but still have to fix the order of some items to have t working as intended. I will post the solution when fixed if anyone wishes to see it.
- 10-20-2012, 06:55 PM #13
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Similar Threads
-
Illegal start of expression
By lodaSchitt in forum New To JavaReplies: 2Last Post: 04-28-2011, 10:04 PM -
Need help with illegal start of expression
By WhopperMan in forum New To JavaReplies: 4Last Post: 10-10-2010, 02:58 AM -
<identifier> expected, illegal start of type
By masberry in forum New To JavaReplies: 1Last Post: 11-02-2009, 07:45 PM -
Illegal Start of Expression
By vince425 in forum New To JavaReplies: 3Last Post: 10-18-2008, 07:41 AM -
illegal start of expression & class, interface, or enum expected
By silverq_82 in forum New To JavaReplies: 9Last Post: 08-08-2007, 07:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks