0

I have a function in which it takes a String, and 2 int. The function, depending on what the string is, performs operations on the 2 numbers.

my default case always runs even though i pass a "+", "-" etc. I dont know why. This is the code:

 public int evaluateAnswerTwoOperations(String operation, int numberOne, int numberTwo) {

    switch(operation){

        case "+":
            Log.d("usingsubtraction","plus2nmbers");
            return numberOne + numberTwo;
        case "-":
            Log.d("usingsubtraction","usingsubtraction");
            return  numberOne - numberTwo;
        case "/":
            Log.d("usingsubtraction","usingdivision");
            return numberOne / numberTwo;
        default:
            Log.d("usingsubtraction","usingmultiplication");
            return numberOne*numberTwo;

    }

Anyone know why?

Harsh
  • 187
  • 2
  • 2
  • 7
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – ΦXocę 웃 Пepeúpa ツ Feb 08 '16 at 19:11
  • 1
    That's not possible, check that the deployed code is up to date, also check the input for trailing/leading spaces. `return` is `return` no code is ever run after (unless you use a try/finally) –  Feb 08 '16 at 19:11
  • @anaxin That would require using `char` instead of `String`. – Code-Apprentice Feb 08 '16 at 19:14
  • The code you give here looks correct as far as I can tell, so the error is likely somewhere else. I suggest adding a `Log.d()` call to print the value of `operation`. You should include quotes around the value so that you can see whitespace that might be causing the problem. – Code-Apprentice Feb 08 '16 at 19:16
  • Do you think it's doing something different than what it is actually doing because you are logging the wrong thing? `Log.d("usingsubtraction","plus2nmbers");` – mkasberg Feb 08 '16 at 19:16
  • Doesn't look like anything is wrong here. It would be great, if you can come up with a small program to reproduce this. – KKishore Feb 08 '16 at 19:17
  • 3
    Unsolicited suggestion: You should explicitly have a `case "*":` for multiplication and use `default:` to print an appropriate error message. – Code-Apprentice Feb 08 '16 at 19:17
  • What JDK version do you have? try using char instead of String. I think it is not supported in older versions – anaxin Feb 08 '16 at 19:26

4 Answers4

0

Since you're only using String of length 1 for each switch cases, I would rather use a char instead. So try the following:

public int evaluateAnswerTwoOperations(char operation, int numberOne, int numberTwo) {

    switch(operation){

        case '+':
            Log.d("usingsubtraction","plus2nmbers");
            return numberOne + numberTwo;
        case '-':
            Log.d("usingsubtraction","usingsubtraction");
            return  numberOne - numberTwo;
        case '/':
            Log.d("usingsubtraction","usingdivision");
            return numberOne / numberTwo;
        default:
            Log.d("usingsubtraction","usingmultiplication");
            return numberOne*numberTwo;

    }
}
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
  • 1
    While this is definitely a good suggestion, it doesn't change the expected behavior nor point out what is incorrect in the OP's original code. – Code-Apprentice Feb 08 '16 at 19:15
0

If the code is always running the default case, then the string being passed in does not equal any of the other cases, compared as with String.equals(). Dump the value of that parameter, perhaps enclosed in angle brackets to display whitespace and/or maybe its hex representation, to see what is getting there as opposed to what you think is getting there...

arcy
  • 12,845
  • 12
  • 58
  • 103
0

This problem can be caused by Following Problems:

  1. Your String is not comparable with any of your cases.
  2. The values you are passing in function does contains some vulnerable value

So, all you need is to check if you are passing correct values in functions. This can be avoided by Passing CHAR instead of STRING into function (which is good approach for +, -, / and *) ... Use this public int evaluateAnswerTwoOperations(char operation, int numberOne, int numberTwo) and do rest of the comparisons.

Other Possible Solution to your problem can be:

Instead of Multiplying in Default case You must introduce another Switch case for comparing with *. And in default case Print an Error.. So if neither of case get a perfect match.. Error will be printed

Hassnain
  • 154
  • 1
  • 2
  • 17
0

Traditionally, switch was possible only for primitive types, like int, long, char, short and enum. I do not know for sure, but as far as I am not mistaken, Android does not support switch with String in the newest version either, but certainly does not support it for older versions, so, as other answers suggested, you might want to use and pass char instead of String, due to the lack of support for the operation.

However, on Java 7, switch with String is already supported. Example:

package dustin.examples;

import static java.lang.System.out;

/**
 * Simple class demonstrating switch on Strings available with JDK 7.
 */
public class StringsWithSwitchDemo
{
   /**
    * Main executable function.
    *
    * @param arguments Command-line arguments: none expected.
    */
   public static void main(final String[] arguments)
   {
      final String name =   arguments.length > 0
                          ? arguments[0]
                          : "Dustin";

      switch (name)
      {
         case "Dino" :
            out.println("Flintstones?");
            break;
         case "Neo" :
            out.println("Matrix?");
            break;
         case "Gandalf" :
            out.println("Lord of the Rings?");
            break;
         case "Dustin" :
            out.println("Inspired by Actual Events");
            break;
         default :
            out.println("The Good, the Bad, and the Ugly?");
      }
   }
}

Source.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175