-3

All i want is to return true if x and y are both "true".

I want to use the switch/case method, instead of a simple if/else.

But i can't get the method to accept my return result.

Do you guys have an idea?

 public class test2 {
     public static boolean f(boolean x, boolean y){
        switch(x + "-" + y){            
        case "true-false": 
             return false; //i also tried "return x" or "return y"
        case "false-true": 
             return false;
        case "true-true": 
             return true;
        case "false-false": 
             return false; 
        }
    }


    public static void main(String[]args){
        f(false,true);
    }   
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
JavaPSPSPS
  • 35
  • 6
  • 11
    Why don't you return `x && y`? – Turing85 Dec 30 '19 at 12:28
  • The reason this fails is explained here: https://stackoverflow.com/a/513839/11389043 (but I agree with Turing85, you shouldn't do this). – Alex B Dec 30 '19 at 12:29
  • 2
    @AlexB this is not true. If `String`s are used in `switch`, [they are compared as if `equals(...)` is used](https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html) – Turing85 Dec 30 '19 at 12:32
  • Didn't know (or expect) this behavior. Thanks! (But now I wonder why the original code didn't work, going to test this now :) ) – Alex B Dec 30 '19 at 12:35
  • @Turing85 Oh man, yes. That is way easier. – JavaPSPSPS Dec 30 '19 at 12:35

5 Answers5

2

First off, the program as-is does not compile. The compiler will complain that method f is missing a return statement. The switch is exhaustive, however the compiler is unable to infer this exhaustiveness and thus complains that there is a missing return if no case matches. One possible solution is to add a default case as RamPrakash shows in his answer. Another possibility is to add a return at the end of the method. In my opinion, both solutions are sub-optimal, more on this later.

With the given fix, the code works as expected. The result is not shown on screen since it is never printed. A slightly modified version of the program gives the expected result:

class Ideone {
  public static boolean f(final boolean x, final boolean y) {
    switch (x + "-" + y) {
      case "true-false":
        return false;
      case "false-true":
        return false;
      case "true-true":
        return true;
      case "false-false":
        return false;
    }
    return false;
  }

  public static void main(final String[] args) {
    System.out.println(f(false, false));
    System.out.println(f(false, true));
    System.out.println(f(true, false));
    System.out.println(f(true, true));
  }
}

Ideone demo


I would suggest a different solution: instead of constructing a String-representation of both parameters, one could just return the AND-product of both parameters: x && y. For one, this gets rid of the sub-optimal return (albeit it on the end of the method or in the default-case). For another, this is more efficient, wrt. computational time and memory consumption.

Turing85
  • 18,217
  • 7
  • 33
  • 58
0

Your code is too complicated and could be simply changed to:

 public class test2 {
 public static boolean f(boolean x, boolean y){
    return x && y;
}


public static void main(String[]args){
    f(false,true);
}   

}

Misha Lemko
  • 591
  • 4
  • 9
0

There should be a default return statement, it will not compile because it's asking return statement.

public class Test {
   public static boolean f(boolean x, boolean y){
        switch(x + "-" + y){
            case "true-false":
                return false; //i also tried "return x" or "return y"
            case "false-true":
                return false;
            case "true-true":
                return true;
            case "false-false":
                return false;
            default: return false;
        }
    }

    public static void main(String[]args){
        System.out.println(f(false,true));
    }
}
Neera
  • 76
  • 5
-1

Firstly your code won't compile and also it is not an efficient algorithm.

An efficient algorithm should look like return x && y instead of using switch in string.

No need to convert boolean to string. There are various ways of achieving what you are trying to do.

public static boolean f(boolean x, boolean y){
    return x && y;
}

Fix compile error by approach #1

Add default case to switch, something like below

public static boolean f(boolean x, boolean y){

     switch(x + "-" + y){

        case "true-false": 
             return false; //i also tried "return x" or "return y"
        case "false-true": 
             return false;
        case "true-true": 
             return true;
        case "false-false": 
             return false; 
        default:
            return false;
      }

}

Fix compile error by approach #2

 public static boolean f(boolean x, boolean y){

     switch(x + "-" + y){

            case "true-false": 
                 return false; //i also tried "return x" or "return y"
            case "false-true": 
                 return false;
            case "true-true": 
                 return true;
            case "false-false": 
                 return false; 
          }
         return false;
    }
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
  • 1
    The cases are exhaustive. The `default`-case is thus superfluous. This does not solve the core-problem. – Turing85 Dec 30 '19 at 12:36
  • Thank you! Now i know that return x && y is way easier, but if i want to use switch/case, all i need to do is add the "default". You guys rock – JavaPSPSPS Dec 30 '19 at 12:38
  • 1
    @Turing85 Error is caused because of missing default case in this scenario. I agree there is better way of coding this. I try to explain him why switch statement errored out. – RamPrakash Dec 30 '19 at 12:40
  • @Turing85 Doesn't it? Now my program runs. If x and y are true, then my program returns true. But of course x&&y is way better. – JavaPSPSPS Dec 30 '19 at 12:40
  • The code in the question works as expected, without default, see https://repl.it/repls/GraveCoordinatedGoals. The only thing missing is a return statement (or default) statement to make it compile. In other words: the problem is a compilation problem, not a logic problem. – Alex B Dec 30 '19 at 12:40
  • @JavaPSPSPS better way to code is x&&y.There are various ways of achieving that you are trying todo. I explained you with and answer why your code failed. – RamPrakash Dec 30 '19 at 12:42
  • While `default` fixes the issue, it is not the only way. Fact is, OPs code does not copile as-is. See [my answer](https://stackoverflow.com/a/59531130/4216641) for details. – Turing85 Dec 30 '19 at 12:50
-1

add default in the end of case statement.

default:
   return true;

and then to see your function returning result in console, use System.out.println() to see in console.

System.out.println(f(false,true));