Results 1 to 8 of 8
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Switch Case statement

    Can I have a Switch case statement inside another? For ex.

    Java Code:
    switch(variableOne){
    case 1:
       blah blah blah blah;
       blah blah blah blah;
             switch (variableTwo){
                     case 1:
                           blah blah blah blah;
                           blah blah blah blah;
                           break;
                     case 2:
                           blah blah blah blah;
                           blah blah blah blah;
                           break;
                          }
    break;
    case 2:
       blah blah blah blah;
       blah blah blah blah;
             switch (variableThree){
                     case 1:
                           blah blah blah blah;
                           blah blah blah blah;
                           break;
                     case 2:
                           blah blah blah blah;
                           blah blah blah blah;
                           break;
                          }
    break
    default:
    System.out.println("Error");
    }

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    of course. Why can't you
    Try this same code. It'll work perfectly fine

  3. #3
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Well when i use
    Java Code:
    System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n", "1. Circle", "2. Rectangle", "3.Square", "4. Triangle", "5. Cone", "Cylinder");
    		int selectionOne = input.nextInt();
    		
    		switch(selectionOne)
    		{
    			case 1:
    				System.out.println("What is the Radius of the Circle? ");
    				int R1 = input.nextInt();
    				System.out.println("What is the Diameter of the Circle? ");
    				int D1 = input.nextInt();
    				Circle C1 = new Circle(D1, R1);
    				System.out.println("1 for Area, and 2 for Perimeter: ");
    				int asking = input.nextInt();
    				Switch(asking){
    					case 1:
    						System.out.println(C1.getArea());
    						break;
    					case 2:
    						System.out.println(C1.getPerimeter());
    						break;
    					default:
    						System.out.println("System Error");
    				}
    		}
    I'm getting duplicate case errors???

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    I don't find anything wrong still.
    Just to make sure I copied your code and compiled and ran. No problems, except that second switch is misspelled (S is in upper case).

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by seanfmglobal View Post
    I'm getting duplicate case errors???
    In your original example it looked you had a duplicate case (2) error in your outer switch statement because of misleading indentation. Please post your code verbatim, not a sloppily crafted copy thereof; we have to see what you saw.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

    Default

    You spelled Switch with a capital S. Its lowercase.

  7. #7
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Thanks a lot quad.... I can't believe I missed that lol very embarrassing.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

Similar Threads

  1. switch case with integers and strings...
    By JavaComplient in forum New To Java
    Replies: 2
    Last Post: 10-21-2010, 07:52 AM
  2. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 06:56 PM
  3. [SOLVED] Making Switch Case Insensitive?
    By iPetey in forum New To Java
    Replies: 6
    Last Post: 04-09-2009, 05:46 PM
  4. Replies: 2
    Last Post: 02-23-2009, 01:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-27-2009, 12:20 AM

Tags for this Thread

Posting Permissions

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