Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Syntax error on Token else?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Syntax error on Token else?

    So I wrote this program for a lab assignment and I tried to compile it and I got this syntax error - "1 error found: File: C:\Users\Documents\Unit6myName.java [line: 34] Error: Syntax error on token "else", ) expected" Could anyone help me out with this?

    import java.util.*;
     
    public class Unit6MYNAME
    {
      public static final double SAVE_INTEREST = .04;
      public static final double NORM_CHECK_INTEREST = .015;
      public static final double MAX_CHECK_INTEREST = .03;
      public static final double CHECK_FEE = 25.00;
      public static final double SAVE_FEE = 10.00;
      public static final double CHECK_MINI_BAL = 25.00;
      public static final double SAVE_MINI_BAL = 500.00;
     
      public static void main(String[] args)
      {
        Scanner keyboard = new Scanner(System.in);
     
        String accountNumber,
               accountType,
               continuation;
     
        double preAccountBalance,
               accountBalance,
               interest;
     
     
        while (continuation.equalsIgnoreCase("y"))
        {
           System.out.println("Please enter y to continue, ");
           System.out.println("otherwise enter n to quit.");
           continuation = keyboard.nextString;   
     
           if (accountNumber.matches("\\d{10}")
     
           else
            System.out.println("Invalid Entry");
            System.exit(0);
     
           System.out.println("Please enter the letter c to enter information");
           System.out.println(" for a checking account."); 
           System.out.println("Otherwise, enter s for a savings account.");
           accountType = keyboard.nextLine;
     
           if (accountType.equalsIgnoreCase("c"))
           {    
             System.out.println("Please enter the account balance.");
             preAccountBalance = keyboard.nextDouble;
     
             if (preAccountBalance < CHECK_MINI_BAL)
             { 
               preAccountBalance = preAccountBalance - CHECK_FEE;
               interest = 0;
             }
     
             else 
              interest = preAccountBalance * NORM_CHECK_INTEREST;
             accountBalance = preAccountBalance + interest;
           }
     
             else if (accountBalance >= 5500)
              interest = preAccountBalance * MAX_CHECK_INTEREST;
              accountBalance = preAccountBalance + interest;
     
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < CHECK_MINI_BAL)
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
     
             System.out.println("New Balance: .2f" , accountBalance);
           }
     
           if (accountType.equalsIgnoreCase("s"))
           {    
             System.out.println("Please enter the account balance.");
             accountBalance = keyboard.nextDouble;
     
             if (preAccountBalance < SAVE_MINI_BAL)
             { 
               preAccountBalance = accountBalance - SAVE_FEE;
               interest = 0;
             }
     
             else 
              interest = preAccountBalance * SAVE_INTEREST;
              accountBalance = preAccountBalance + interest;
     
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < SAVE_MINI_BAL)
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
     
             System.out.println("New Balance: .2f" , accountBalance);
           }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    ) expected
    I think this is the key part of the message. The compiler thinks there should be a ) at the indicated location.
    Check the code for properly pairing of ( and ) and see why the compiler thinks there is a missing ).
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    I tried adding an additional parentheses, but then the error changed to saying that there is an error on the else token, delete this token.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    The compiler often only finds one error at a time because that error hides the next error(s). A missing ) is a big deal and needs to be fixed so the rest of the tokens can be properly placed.

    What is the new error on the else token? Please copy the full text of the error message and paste it here.


    One problem I see with the code is there are some if statements that do NOT have {}s following them to enclose the statements controlled by the if. You should ALWAYS use {}s with if and else statements.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    1 error found:
    File: C:\Users\Documents\MYNAME.java [line: 34]
    Error: Syntax error on token "else", delete this token

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    The compiler thinks that the else is misplaced and should be removed.

    Did you see the following?

    One problem I see with the code is there are some if statements that do NOT have {}s following them to enclose the statements controlled by the if. You should ALWAYS use {}s with if and else statements.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    Don't I need an else there?

    And yes, I added more {}s.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    Don't I need an else there?
    I don't know. Are the compiler errors fixed now?

    Post your current code if you have questions about it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    Alright, I did a lot of fiddling with my code and got it to compile, but now it is shutting down prematurely. For some reason, my program seems to skip over several segments of code and immediately perform System.exit.

    import java.util.*;
     
    public class Unit6ParkerTom
    {
      public static final double SAVE_INTEREST = .04;
      public static final double NORM_CHECK_INTEREST = .015;
      public static final double MAX_CHECK_INTEREST = .03;
      public static final double CHECK_FEE = 25.00;
      public static final double SAVE_FEE = 10.00;
      public static final double CHECK_MINI_BAL = 25.00;
      public static final double SAVE_MINI_BAL = 500.00;
     
      public static void main(String[] args)
      {
        Scanner keyboard = new Scanner(System.in);
     
        String accountNumber = "0000000000";
        String       accountType = "g";
     
          int continuation;
     
        double preAccountBalance =0,
               accountBalance = 0,
               interest = 0;
     
         System.out.println("Please enter a non zero integer to continue, ");
         System.out.println("otherwise enter 0 to quit.");
         continuation = keyboard.nextInt();   
     
     
     
        while (continuation != 0)
        {
           System.out.println("Please enter an account number.");
           accountNumber = keyboard.nextLine();
     
           if (accountNumber.matches("\\d{10}"))
           {
            System.out.println("Please enter the letter c to enter information");
            System.out.println(" for a checking account."); 
            System.out.println("Otherwise, enter s for a savings account.");
            accountType = keyboard.nextLine();
           }
           else
           {
            System.out.println("Invalid Entry");
            System.exit(0);
           }  
     
     
           if (accountType.equalsIgnoreCase("c"))
           {    
             System.out.println("Please enter the account balance.");
             preAccountBalance = keyboard.nextDouble();
     
             if (preAccountBalance < CHECK_MINI_BAL)
             { 
               preAccountBalance = preAccountBalance - CHECK_FEE;
               interest = 0;
             }
     
             else 
             { 
              interest = preAccountBalance * NORM_CHECK_INTEREST;
              accountBalance = preAccountBalance + interest;
             }
           }
     
             else if (accountBalance >= 5500)
              interest = preAccountBalance * MAX_CHECK_INTEREST;
              accountBalance = preAccountBalance + interest;
     
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < CHECK_MINI_BAL)
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
     
             System.out.printf("New Balance: .2f" , accountBalance);
           }
     
           if (accountType.equalsIgnoreCase("s"))
           {    
             System.out.println("Please enter the account balance.");
             accountBalance = keyboard.nextDouble();
     
             if (preAccountBalance < SAVE_MINI_BAL)
             { 
               preAccountBalance = accountBalance - SAVE_FEE;
               interest = 0;
             }
     
             else 
              interest = preAccountBalance * SAVE_INTEREST;
              accountBalance = preAccountBalance + interest;
     
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < SAVE_MINI_BAL)
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
     
             System.out.printf("New Balance: .2f" , accountBalance);
     
           System.out.println("Please enter a non zero integer to continue, ");
           System.out.println("otherwise enter 0 to quit.");
           continuation = keyboard.nextInt();   
           }
        }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    I still see lots of if and else statements that do NOT have the following statements enclosed in {}s.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    All curly brackets added. Program is still skipping to System.exit.


    import java.util.*;
     
    public class Unit6ParkerTom
    {
      public static final double SAVE_INTEREST = .04;
      public static final double NORM_CHECK_INTEREST = .015;
      public static final double MAX_CHECK_INTEREST = .03;
      public static final double CHECK_FEE = 25.00;
      public static final double SAVE_FEE = 10.00;
      public static final double CHECK_MINI_BAL = 25.00;
      public static final double SAVE_MINI_BAL = 500.00;
     
      public static void main(String[] args)
      {
        Scanner keyboard = new Scanner(System.in);
     
        String accountNumber = "0000000000";
        String       accountType = "g";
     
          int continuation;
     
        double preAccountBalance =0,
               accountBalance = 0,
               interest = 0;
     
         System.out.println("Please enter a non zero integer to continue, ");
         System.out.println("otherwise enter 0 to quit.");
         continuation = keyboard.nextInt();   
     
     
     
        while (continuation != 0)
        {
           System.out.println("Please enter an account number.");
           accountNumber = keyboard.nextLine();
     
           if (accountNumber.matches("\\d{10}"))
           {
            System.out.println("Please enter the letter c to enter information");
            System.out.println(" for a checking account."); 
            System.out.println("Otherwise, enter s for a savings account.");
            accountType = keyboard.nextLine();
           }
           else
           {
            System.out.println("Invalid Entry");
            System.exit(0);
           }  
     
     
           if (accountType.equalsIgnoreCase("c"))
           {    
             System.out.println("Please enter the account balance.");
             preAccountBalance = keyboard.nextDouble();
     
             if (preAccountBalance < CHECK_MINI_BAL)
             { 
               preAccountBalance = preAccountBalance - CHECK_FEE;
               interest = 0;
             }
     
             else 
             { 
              interest = preAccountBalance * NORM_CHECK_INTEREST;
              accountBalance = preAccountBalance + interest;
             }
           }
     
             else if (accountBalance >= 5500)
             {
              interest = preAccountBalance * MAX_CHECK_INTEREST;
              accountBalance = preAccountBalance + interest;
             } 
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < CHECK_MINI_BAL)
             {
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
             }
             System.out.printf("New Balance: .2f" , accountBalance);
           }
     
           if (accountType.equalsIgnoreCase("s"))
           {    
             System.out.println("Please enter the account balance.");
             accountBalance = keyboard.nextDouble();
     
             if (preAccountBalance < SAVE_MINI_BAL)
             { 
               preAccountBalance = accountBalance - SAVE_FEE;
               interest = 0;
             }
     
             else 
             {
              interest = preAccountBalance * SAVE_INTEREST;
              accountBalance = preAccountBalance + interest;
             }
             System.out.println("Account Number:" + accountNumber);
             System.out.println("Type of Account:" + accountType);
             System.out.printf("Previous Balance: .2f" , preAccountBalance);
             System.out.printf("Interest Earned: .2f" , interest);
     
             if (preAccountBalance < SAVE_MINI_BAL)
             {
               System.out.printf("Service Fee: .2f" , CHECK_FEE);
             }
             System.out.printf("New Balance: .2f" , accountBalance);
     
           System.out.println("Please enter a non zero integer to continue, ");
           System.out.println("otherwise enter 0 to quit.");
           continuation = keyboard.nextInt();   
           }
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    what value of input is causing the code to execute the System.exit()? Add the variable at the end of the println() so it is printed out.

    Can you copy the full contents of the console window from when you execute the program that shows everything printed out by the program and everything entered by the user?

    If you are using the Scanner methods nextInt() and nextLine() intermixed then you are probably having a problem with the endline char being left in the Scanner's buffer . Add a call to nextLine() after a call to nextInt() to clear the endline char.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    It should end if a ten digit account number is not entered properly, however it executes System.exit() after the first prompt to continue. As for the second part, not sure what you are referencing.

    Entering any non zero digit at the input box causes the program to execute System.exit and everything disappears from the console window.

    > run Unit6MYNAME
    Please enter a non zero integer to continue,
    otherwise enter 0 to quit.
    [DrJava Input Box]

    How do I do that?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    Sorry, I don't know anything about DrJava.

    What prints out for the invalid input that causes the program to exit?
    Invalid Entry ???????
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    SgtGarrison (March 19th, 2013)

  16. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error on Token else?

    Quote Originally Posted by Norm View Post
    Sorry, I don't know anything about DrJava.

    What prints out for the invalid input that causes the program to exit?
    Invalid Entry ???????
    Yeah, Invalid Entry is supposed to print out if the account number is not ten digits and then System.exit is supposed to execute. However, it skips from the first prompt past the account number segment in the while loop all the way to the System.exit command without even displaying the invalid entry message.

    Edit: I can see the invalid entry message now. :/

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax error on Token else?

    I can see the invalid entry message now
    What prints out at the end of the invalid input messages that shows what causes the program to exit?
    Invalid Entry ???????
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Syntax error on token "extends", throws expected
    By Purple01 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 16th, 2012, 07:29 AM
  2. Syntax error on token ";", { expected after this token please HELP
    By Creeper in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 1st, 2012, 03:12 PM
  3. Please help not sure how to get past the Syntax error
    By KnightC in forum Other Programming Languages
    Replies: 2
    Last Post: December 23rd, 2011, 10:09 AM
  4. MySQL Syntax Error
    By tarkal in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 03:29 PM
  5. Syntax error on token ";", @ expected after this token
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2011, 07:48 AM