Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: "Orphaned Default"?

  1. #1
    oko_23 is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default "Orphaned Default"?

    Hello. I am very new to Java and have just learned about switch statements. Here is the program I was just working on:

    import java.util.Scanner;
    class calc{
    public static void main(String args[]){
    Scanner inpt = new Scanner(System.in);
    int num;
    double fnum, snum, ansr;
    System.out.println("Please input the first number of your equation.");
    fnum = inpt.nextDouble();
    System.out.println("Next, choose an operator-'1' for addition, '2' for subtraction, '3' for multiplication, and '4' for division.");
    num = inpt.nextInt();
    Switch (num){
    default:
    System.out.println("Sorry, that number is not recognized. Please restart the program.");
    break;
    case 1:
    System.out.println("Now, enter the second addend of your desired equation.");
    snum = inpt.nextDouble();
    ansr = fnum + snum;
    System.out.print("Here is your sum: ");
    System.out.println(ansr);
    }
    }
    }

    Whenever I run it, I get his message:

    t.java:11:error: ';' expected
    Switch (num){
    ^
    t.java:12: error: orphaned default
    default:
    ^
    2 errors


    Thank you for your help!

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

    Default Re: "Orphaned Default"?

    Please edit (or repost) your code: properly indented and between the correct [code] ... [/code] tags, because as it is now, the code is hardly readable.
    b.t.w. it's 'switch', not 'Switch' ...

    kind regards,

    Jos
    Kagiso likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: "Orphaned Default"?

    Another issue: the default is used to catch values that do not match all other cases. How can this work if you put the default first?

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

    Default Re: "Orphaned Default"?

    Quote Originally Posted by Junky View Post
    Another issue: the default is used to catch values that do not match all other cases. How can this work if you put the default first?
    The order of the case clauses is not important (except for the fall-through clauses), i.e. the 'default' clause might as well come first (although it looks funny).

    kind regards,

    Jos
    Kagiso likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: "Orphaned Default"?

    I didn't know that. I thought switch was like an if and it entered the first branch where the condition was met.

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

    Default Re: "Orphaned Default"?

    Quote Originally Posted by Junky View Post
    I didn't know that. I thought switch was like an if and it entered the first branch where the condition was met.
    If a switch statement were no more than a chain of if-else startements, it wouldn't be faster than such a chain; compiling a switch statement takes quite some effort from the compiler: first if collects the set of case clauses (the int constants); if that set is 'near' optimal, i.e. dense around zero, e.g. 1, 2, 3, 4 etc. if creates a jump table where the case clause constants make up the index in the jump table. If the set is not so dense, it creates a sorted search table where the jump address can be found during runtime by doing a binary chop on the table. Each table (the jump table or the search table) has default entries (if the switch expression wasn't found in the table). See also the JVM instruction set for the two switch instructions (one for each table type). That stupid 'swith on a String' wish is a real burden ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Error Compiling "Orphaned Case"
    By RebellionX in forum New To Java
    Replies: 3
    Last Post: 05-27-2012, 04:56 PM
  4. Replies: 2
    Last Post: 01-29-2009, 07:17 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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