0

Possible Duplicate:
Comparing two identical strings with == returns false

I am having real hard time with solving this code. This might look silly but I am not able to figure out what is happening. I am parsing a JSONArray(No big deal!) I am traversing the array with a for loop. I want to break the loop when the user input and the value matches. Here is my code

String regi = null;
JSONObject studentObject = null;
try {
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
    studentObject = returned.getJSONObject(i);
    regi = studentObject.getString("REGISTRATION_NO");
    if (regi == reg) {
        name = studentObject.getString("STUDENT_NAME");
        break;
    }
}
course = studentObject.getString("COURSE_CODE");
Log.d("Details", name + course + regi + i);//Out put: nullGraduate081018394:name - null
//course: Graduate (same for all), regi: last registration number, 
//i: giving totalnumber of objects

As per my knowledge the loop should stop when it finds a match. The COURSE_CODE will be corresponding to the student. Am I missing something?

Please note: The function getInternetData() is returning the whole JSON Array. The loop is completely traversing every object.

Community
  • 1
  • 1
  • I am not sure which line is causing trouble. I think my case is bit different – Dibya Panda Oct 05 '12 at 19:31
  • what is name here that you are printing in log? It doesnt have any data (it is a null variable as from the post you did) – G_S Oct 05 '12 at 19:32
  • @Sharath I am sure I am not comparing null variables. Which varible is null can you please point out? – Dibya Panda Oct 05 '12 at 19:34
  • its not comparing but just try changing this line Log.d("Details", name + course + regi + i) to Log.d("Details", name + " - "+course +" - "+ regi +" - "+ i) and be back with the output you are getting in log – G_S Oct 05 '12 at 19:36
  • Doesn't change much. I am getting something like this: `null`-`Graduate`-`081018394`-`18`. Where as I should get my name in place of null, my registration number and my slno. I am getting all details form last object and name turns to be null. – Dibya Panda Oct 05 '12 at 19:39
  • and even change if (regi == reg) { name = studentObject.getString("STUDENT_NAME"); break; } to any of the suggested ones below. The one you wrote just compares memory locations as far as i know and not the content – G_S Oct 05 '12 at 19:39
  • So sorry server was not responding. After changing the `==` it works. Thanks – Dibya Panda Oct 05 '12 at 19:43

5 Answers5

2

Strings cannot be compared with == in Java. You have to use string1.equals(string2).

Perley
  • 292
  • 1
  • 3
  • 12
1

Use regi.equals(reg) or regi.contentEquals(reg) instead of == and you will be fine :-)

slezadav
  • 6,104
  • 7
  • 40
  • 61
1

use regi.contentEquals(reg) or !regi.contentEquals(reg) for comparison

CQM
  • 42,592
  • 75
  • 224
  • 366
1

you should use regi.contentEquals(reg)

Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • String hi = "hi"; String hello = "hi"; if(hi==hello){ //do something } This works. – Dibya Panda Oct 05 '12 at 19:27
  • And this tends to not: `char c = 'i'; String a = "hi"; String b = "h" + c; if (a == b) { //something }`. `==` does an object comparison, whereas `equals()` does a `String` comparison. Read through the actual answer I linked. – kabuko Oct 05 '12 at 19:36
  • OK then try the sysout way... System.out.println("regi: -" + regi + "- reg: -" + reg + "-"); and see if you can spot any weird thing :) – Apostolos Oct 05 '12 at 19:43
  • @DibyaPanda: Java interns String literals. So when you create to String literals `String a = "..."; String b = "..."`, it first adds the String "..." to a table of interned Strings, then when you create the second String b, it looks up "...", sees it in the table of intered Strings, and b will point to the same String a does. However, when you are not literally declaring Strings, this will not always work. == checks to see if two primitives have same value or if two objects point to the same underlying object. – aamit915 Oct 05 '12 at 19:54
0

try using this

JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
// added the below line
studentObject = new JsonObject();
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi.equals(reg)) {
    name = studentObject.getString("STUDENT_NAME");
    break;
}
}

instead of just

JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi == reg) {
    name = studentObject.getString("STUDENT_NAME");
    break;
}
}
G_S
  • 7,068
  • 2
  • 21
  • 51