Results 1 to 13 of 13
  1. #1
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Question 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Compile errors: illegal start of expression, not a statement, ';' expected

    Java Code:
        } // end method PlayAgain
     
    } // end class
    }
    What's that stray } doing at the end?

    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.

  4. #4
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default 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...

  5. #5
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Compile errors: illegal start of expression, not a statement, ';' expected

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
         } // end method PlayAgain
     
    } // end class
    }
    What's that stray } doing at the end?

    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.
    Sorry - I should've put that bracket above the last one or move the remark.

  6. #6
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  7. #7
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default 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 12
    Last edited by Lionlev; 10-18-2012 at 09:10 PM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  8. #8
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default 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 class
    WARNING I am Russian so it's possible that I wont understand you correctly...

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default 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.

    ** This space for rent **

  10. #10
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  11. #11
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default 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

    :D
    WARNING I am Russian so it's possible that I wont understand you correctly...

  12. #12
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  13. #13
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Compile errors: illegal start of expression, not a statement, ';' expected

    Sure, let me know
    :D
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Illegal start of expression
    By lodaSchitt in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 10:04 PM
  2. Need help with illegal start of expression
    By WhopperMan in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 02:58 AM
  3. <identifier> expected, illegal start of type
    By masberry in forum New To Java
    Replies: 1
    Last Post: 11-02-2009, 07:45 PM
  4. Illegal Start of Expression
    By vince425 in forum New To Java
    Replies: 3
    Last Post: 10-18-2008, 07:41 AM
  5. Replies: 9
    Last Post: 08-08-2007, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •