-2

I'm trying to understand a answer given, but I dont understand why the answer is "Hello World", hope for someone to explain.

public class TestClass{
  public static int getSwitch(String str){
      return (int) Math.round( Double.parseDouble(str.substring(1, str.length()-1)) );
  }
  public static void main(String args []){
    switch(getSwitch(args[0])){
      case 0 : System.out.print("Hello ");
      case 1 : System.out.print("World"); break;
      default : System.out.print("Good Bye");
    }
  }
}

What will be printed by the above code if it is run with command line: java TestClass --0.50 (There are two minuses before 0.)

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
Mônica R.
  • 225
  • 2
  • 13
  • 1
    What does the `getSwitch` function return? Also, you forgot to put `break;` after the `case 0:` clause. – The Coding Monk Sep 12 '15 at 11:20
  • @TheCodingMonk OP didn't write the code, and the `break` was deliberately left out as a exercise to show what a missing break means. – Andreas Sep 12 '15 at 12:02

3 Answers3

1

According to JavaDocs Double

parseDouble(String s)

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

So change your code:

public static int getSwitch(String str){
return (int) Math.round( Double.parseDouble(str.substring(1, str.length()-1)));
  }

with:

public static long getSwitch(String str){
return Math.round(Double.parseDouble(str));
  }

because round(double) return long JavaDocs Math Round

round(double a)

Returns the closest long to the argument, with ties rounding up.

Then replace main with:

  public static void main(String args []){
      switch(getSwitch(args[0])){
      case 0 : System.out.print("Hello");
      break;
      case 1 : System.out.print("World"); 
      break;
      default : System.out.print("Good Bye");
      break;
    }
  }

And if you run class with 0 it print Hello and with 1 print World, default operation prints Good Bye in according to JavaDocs Switch Satement

The default section handles all values that are not explicitly handled by one of the case sections.

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
1
public static int getSwitch(String str) {        // str = "--0.50"
    String x = str.substring(1, str.length()-1); // x = "-0.5"
    double y = Double.parseDouble(x);            // y = -0.5
    long z = Math.round(y);                      // z = 0   (ties rounding to positive infinity)
    return (int) z;                              // returns 0
}
public static void main(String[] args) {
    switch (getSwitch("--0.50")) {
        case 0:  System.out.print("Hello ");       // executed because getSwitch returned 0
        case 1:  System.out.print("World"); break; // executed because case 0 didn't end with a break
        default: System.out.print("Good Bye");     // not executed because case 1 did end with a break
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Your code already has the answer as a comment.

What will be printed by the above code if it is run with command line: java TestClass --0.50 (There are two minuses before 0.)

That means if you run your code trough command line an pass the said argument it will then be stored in String[] args at position 0 because its your only and first argument.

After that your algorithm tests the String and tries to convert it to double, which will then be tested in your switch case.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Einstein
  • 360
  • 1
  • 4
  • 15