Results 1 to 10 of 10

Thread: else without if

  1. #1
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default else without if

    hi all

    i have almost finished this program but the end of it the else is coming up with 'else without if' but if i put a brace in it says illegall start can anyone see the problem

    Java Code:
    //This program will provide a menu system for inputting
    //data regards 3 taxi's
    import java.util.Scanner;//importing the scanner utility
    
    public class c22taxis {
        static int preference;
        static Scanner input = new Scanner(System.in);
        static String[] name = new String[3];
        static String[] dest = new String[3];
        static String[] service = new String[3];
        static int tNo = 0;
        static int cont = 0;
        
        public static void main(String[] args) {
            menu();// calling the method /main menu
        }
    
        public static void menu(){
            System.out.println('\f');
            // printing of the system menu on screen to user
            System.out.println("Welcome to C22 Taxis automated System");
            System.out.println("");// blank line
            System.out.println("Please select from a option below to continue.");
            System.out.println("");// blank line
            System.out.println(" 1. Enter/Change Driver Name.");
            System.out.println("");// blank line
            System.out.println(" 2. Enter/Change veichle booking status.");
            System.out.println("");// blank line
            System.out.println(" 3. Enter/Change veichle service status.");
            System.out.println("");// blank line
            System.out.println(" 4. List all veichle status");
            System.out.println("");// blank line
            System.out.println(" 5. Exit");// exit command for program
            System.out.println("");
            System.out.print("Please select a option: ");
            mainAsk();
        }
        public static void mainAsk(){
            preference = input.nextInt();
    
            if (preference == 1)// path of input from user
            {
                System.out.println('\f');
                // sub menu for driver change details
                System.out.println(" Driver name sub Menu");
                System.out.println("");
                System.out.println("");
                System.out.print("Please enter taxi number: ");
                
                String tNo2 = input.next();    // Changed so that if some1 puts in a non-int 
                if (tNo2.contains("1") || tNo2.contains("2") || tNo2.contains("3")){
                    tNo = Integer.parseInt(tNo2);
                } else {
                    tNo = 0;
                }
    
                switch (tNo) {
                case 1:
                    System.out.print("Please enter taxi1 drivers name: ");
                    name[0] = input.next();
                    break;
                case 2:
                    System.out.print("Please enter taxi2 drivers name: ");
                    name[1] = input.next();
                    break;
    
                case 3:
                    System.out.print("Please enter taxi3 drivers name: ");
                    name[2] = input.next();
                    break;
    
                // the default
                default:
                    System.out.print("invalid entry return to menu.");
                    break;
                }
                menu();
            } else if (preference == 2) {
                System.out.println('\f');
                // sub menu start for taxi status
                System.out.println(" Taxi destination sub Menu");
                System.out.println("");
                System.out.println("");
                System.out.print("Please enter taxi number: ");
    
                String tNo2 = input.next();    // Changed so that if some1 puts in a non-int 
                if (tNo2.contains("1") || tNo2.contains("2") || tNo2.contains("3")){
                    tNo = Integer.parseInt(tNo2);
                } else {
                    tNo = 0;
                }
    
                if (tNo == 1) {
                    System.out.print("Please enter taxi1 destination(if no destination enter NONE): ");
                    dest[0] = input.next();
                } else if (tNo == 2) {
                    System.out.print("Please enter taxi2 destination(if no destination enter NONE): ");
                    dest[1] = input.next();
                } else if (tNo == 3) {
                    System.out.print("Please enter taxi3 destination(if no destination enter NONE): ");
                    dest[2] = input.next();
                } // else instruction at end giving invalid input for anything other
                else
                    System.out.println("Invalid entry please restart");
                    menu();
         }  else if (preference == 3) {
                  System.out.println('\f');
                  // sub menu start for taxi status
                  System.out.println(" Taxi service sub Menu");
                  System.out.println("");
                  System.out.println("");
                  System.out.print("Please enter taxi number: ");
    
                  String tNo2 = input.next();    // Changed so that if some1 puts in a non-int 
                  if (tNo2.contains("1") || tNo2.contains("2") || tNo2.contains("3")){
                  tNo = Integer.parseInt(tNo2);
                } else {
                   tNo = 0;
                    }
                      if (tNo == 1) {
                          System.out.print("Is Taxi 1 in service?(yes or no): ");
                          service[0] = input.next();
                    } else if (tNo == 2) {
                          System.out.print("Is Taxi 2 in service?(yes or no): ");
                          service[1] = input.next();
                    } else if (tNo == 3) {
                          System.out.print("Is Taxi 3 in service?(yes or no): ");
                          service[2] = input.next();
                    } // else instruction at end giving invalid input for anything other
                       else
                          System.out.println("Invalid entry please restart");
                          menu();
                          //final instruction option form main menu to print taxi information
            }    else if (preference == 4) {
                     System.out.println('\f');
                     //clear screen instruction
                     System.out.println("Taxi 1 present satus is:-");
                     System.out.println("");
                     System.out.println("Driver is: " + (name[0]));
                     System.out.println("Destination is: " + (dest[0]));
                     System.out.println("Is Taxi 1 in service: " + (service[0]));
                     System.out.println("");
                     System.out.println("");
                     System.out.println("Taxi 2 present satus is:-");
                     System.out.println("");
                     System.out.println("Driver is: " + (name[1]));
                     System.out.println("Destination is: " + (dest[1]));
                     System.out.println("Is Taxi 2 in service: " + (service[1]));
                     System.out.println("");
                     System.out.println("");
                     System.out.println("Taxi 3 present satus is:-");
                     System.out.println("");
                     System.out.println("Driver is: " + (name[2]));
                     System.out.println("Destination is: " + (dest[2]));
                     System.out.println("Is Taxi 3 in service: " + (service[2]));
                     //end of taxi information below is command to wait for input from user too continue.
                     System.out.println("");
                     System.out.println("");
                     System.out.println("Press 1 to continue or 2 to quit");
                     cont = input.nextInt();
                    }
                      switch (cont) {
                          case 1: 
                          menu();
                          break;
                          case 2:
                          System.out.print("Thankyou for using C22 Automated system, Goodbye.");
                          break;
                          //the default
                          System.out.println("Invalid entry please return to menu");
                          break;
                        }
                        
         
            else     {
                System.out.println('\f');
                System.out.println("Thank-you for using c22 Automated system.");
                System.exit(0);
        }
    }
    Last edited by Fubarable; 11-21-2009 at 09:14 PM. Reason: code tags added to improve code readability

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

    Default

    You should print out your code, and using a pencil, count your opening and closing braces to see if they match. Do this, and you'll see that you do in fact have an else hanging out in the breeze.

    I also added code tags to your post above to improve its readability. Please see my signature for more details on how you can do this yourself next post. Much luck!

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Well, thats the problem you get when you have such a large nested if/else statement. To make your code more readable make smaller blocks of code. Maybe something like:

    Java Code:
    If (preference == 1)
        processPreference1();
    else if (preference == 2)
        processPreference2();
    ...
    Or you could even make the above a switch statement.

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    A couple conventions I like to do:
    1) is to always use brace delimiters, even for single line if or else statement bodies. Sometimes if we end up adding more than the one line to the body, it will then need braces anyway. If you are religiously against using braces for all block delimiters, you should consider using python, where there is no physical block delimiters at all, and blocks are indicated by white space depth (# of tabs on a line). - if that isn't a crock i don't know what is.

    2) consider using an editor such as "jEdit", "kedit", or "eclipse" or "crimson editor" , that have, or come with plugins, to visually articulate the stuff between start and end curly braces, that will greatly help you find where the one(s) are missing to cause the imbalance here.

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

    Default

    If things come in pairs - like quotes, parentheses and curly brackets - type them in pairs. It's easier to "back arrow" at the start than it is to try and remember what it is that has to be closed at the end.

  6. #6
    gcampton Guest

    Default

    This is why I always like to have my braces at the same ruler indent so they are easily noticeable and mistakes are rarely made eg:
    Java Code:
    public static Object[] array = new Object[0];
    
    public void functionABC()
    {
        if (condition)
        {
            for (int i=0; i< array.length; i++)
            {
                System.out.printf("%s\n", array[i].toString());
            }
        }
        else if (condition)
        {
            // do something
        }
        else
            System.out.println("Error in: %s", getVarName());
    }

  7. #7
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Im just the orther way around i lik to

    void morningGuys() {
    boolean sleeptWell = true :)
    }

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

    Default

    For a couple of generally accepted indentation styles read this link.

    kind regards,

    Jos

  9. #9
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    I have installed a few plugins on jedit is there any in paticular that would help me out of those plugins

    oh and thanks
    travishein

  10. #10
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Red face Thanks

    Thanks all not got it yet but trying jedit and see if i can find it.

    Also thanks for not telling me the answer direct because I wont learn where i making mistake if dont figure it out myself

    Silver

Posting Permissions

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