0

I'm very new to android and am stuck with something very simple. I'm trying to state that for a variable x (can be a, b or c), there is a corresponding integer value (i.e. 0, 1 and 2)

    String mpee = x;
          int mpef = 0;
          int mpev = 0;

        if (mpee == "a") { mpef = mpev + 0; }
        if (mpee == "b") { mpef = mpev + 1; }
        if (mpee == "c") { mpef = mpev + 2; }

       int mpp = mpef;

The output will be int mpp. There is no problem with the code as far as Eclipse is concerned, but when I run it, the application hangs. The following is the logcat.

06-05 13:29:49.014: E/AndroidRuntime(1608): FATAL EXCEPTION: main
06-05 13:29:49.014: E/AndroidRuntime(1608): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.exa.huy/com.example.exa.huy}: android.content.res.Resources$NotFoundException:
String resource ID #0x0
06-05 13:29:49.014: E/AndroidRuntime(1608):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-05 13:29:49.014: E/AndroidRuntime(1608):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-05 13:29:49.014: E/AndroidRuntime(1608):     at 
android.app.ActivityThread.access$600(ActivityThread.java:141)
06-05 13:29:49.014: E/AndroidRuntime(1608):     at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-05 13:29:49.014: E/AndroidRuntime(1608):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
06-05 13:29:49.014: E/AndroidRuntime(1608):     at android.os.Looper.loop(Looper.java:137)

What did I do wrong?

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
user3492802
  • 179
  • 9
  • 2
    The exception is caused by the fact that you try to use an int as resource-id which doesn't exists. Another errors you do is how you compare strings: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Marco Acierno Jun 05 '14 at 13:40
  • post peace of code where you are trying to perform this operation – akhilesh0707 Jun 05 '14 at 13:42
  • 2
    use equalignore case instead of "==" in android – prakash Jun 05 '14 at 13:47

1 Answers1

0

You must alter the way you are comparing strings - as String is an object, it has a built in compare method equals(), in which the input is a string you want to compare your string too. Using == compares if they are the same object rather than if their strings match, which does not seem like what you intended to do. Also, an if-else block might work better than 3 extra statements, as if an if condition is filled it will not bother to check the rest, and in your case will work as each condition is mutually exclusive.

String mpee = x;
      int mpef = 0;
      int mpev = 0;

    if (mpee.equals("a")) { mpef = mpev + 0; }
    else if (mpee.equals("b")) { mpef = mpev + 1; }
    else if (mpee.equals("c")) { mpef = mpev + 2; }

   int mpp = mpef;

From the LogCat it looks like you are called a reference you can't find. Please post more of your code so we can figure out just exactly what the error is. In this case, it seems like you are calling a reference with the integer value 0, which doesn't exist. Are you trying to reference a certain View in your layout?

krodmannix
  • 845
  • 10
  • 30
  • Thanks a lot for your help. It works perfectly just as I hope it would. – user3492802 Jun 05 '14 at 13:56
  • It I might add, does it make any difference to use if(){}, if(){}, if(){} or better to use the one you use above if(){} else if(){} else if(){} – user3492802 Jun 05 '14 at 13:59
  • Essentially, the task you need in this scenario will be completed either way - because `mpee` can only hold one value (and in this case it will be a, b, or c), only one of the `if` statements will go off. However, in general if you have mutually exclusive checks (such as _will a value be_ a, b, or c rather than _will a value contain_ a, b, or c), `if-else` statements will stop unnecessary code from being executed, and overall increase the performance of your program. – krodmannix Jun 05 '14 at 14:07
  • Thanks. Straight to the point and easy to understand. I appreciate it. – user3492802 Jun 05 '14 at 14:09