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 2 of 2

Thread: switches and else if

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face switches and else if

    hi I am designing a program but need some advise, was wondering if I can put a switch in a switch or a switch and then a else if in it see below for where i am trying to go.


    menu view to user

    1. change bus driver

    2. change bus status

    3 change bus destination

    java idea from selection(please note there is 3 different buses)

    so if user chooses option 1 can i do this
    case 1
    System.out.println("please select bus Number ");
    busNumber = input.nextInt();
     
          if (busNumber ==1)
          System.out.println("Enter driver 1 name ")
          name1= input.next();
     
          name1 = name[0];
     
          else if (busNumber ==2)
          System.out.println("Enter driver 2 name ")
          name2= input.next();
     
          name2 = name[1];
     
          else (busNumber ==3)
          System.out.println("Enter driver 3 name ")
          name3= input.next();
     
          name3 = name[2];
     
    case2
    and then similar questions for destination and status in case3

    i will also have a case 4 but the instruction there will be just to print all the details inputted

    If I cant do this then how can i do it and will i need to bunch all cases and else ifs in one set of {} or several

    your help and input is much appreciated
    Last edited by helloworld922; November 11th, 2009 at 03:10 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: switches and else if

    Yes, you can nest as deep as you want in Java.

    if (condA)
    {
        switch(element1)
        {
              case 1:
                   switch(element2)
                   {
                        case 1:
                             // do stuff
                             break;
                        case 2:
                             // do stuff
                             break;
                   }
                   break;
              case2:
                   if (condB)
                   {
                        // do stuff
                   }
                   else
                   {
                        // do stuff
                   }
              }
         }
    }
    else
    {
         // do stuff
    }