Harsh February 2016

How to accurately compare strings?

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?

Answers


sparky February 2016

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;

    }
}


arcy February 2016

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...


Hassnain February 2016

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


Lajos Arpad February 2016

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.

Post Status

Asked in February 2016
Viewed 1,363 times
Voted 14
Answered 4 times

Search




Leave an answer