0

I'm trying to do something reallllly simple that apparently is extremely difficult in android. I just want to compare two strings to see if they are equal.

I have a temp variable with the value "Location" I have debugged this and it does indeed contain Location... So I tried this at first

if(temp == "Location") { //do something }

But I already know that doesn't work. I then tried all the possible functions for a string such as:

.equals .contains .ignoreCaseEquals etc...

If anyone has any idea what to do please help. This is really getting annoying.

EDIT: Here is the function where I'm comparing the strings for those of you who want to see.

public String[] getData(){ 
    try {
        int tempGroupCount = 0;
        URL food_url = new URL (Constants.SERVER_DINING);
        BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
        temp = my_buffer.readLine();
        // prime read
        while (temp != null ){
            // check to see if readline equals Location
            Log.w("HERasdfsafdsafdsafE", temp);
            // start a new location
            if (temp.equals("Location")
            {
                groups[tempGroupCount] = temp;
                tempGroupCount++;
            }
                Log.w("HERasdfsafdsafdsafE", temp);
                //start for-loop to test to get child info
                //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){
                    //children[groupCount][childrenCount] = temp;
                //}

            temp = my_buffer.readLine();
        }
        my_buffer.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
    }
    return groups;
    }
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
cj1098
  • 1,560
  • 6
  • 32
  • 60

8 Answers8

3

equals does work. If temp.equals("Location") returns false, then your temp variable does not refer to a string with the value "Location".

There may be unprintable characters or other oddities about the string - I suggest you look at the length of the string to check. Alternatively, there can be other characters which look like the ASCII characters, but aren't. In the debugger, try examining the array and get at the underlying char array - check the Unicode value of each character.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well the Data that I'm pulling from is on a server http://mtweb.mtsu.edu/mobiledev2/Dining/Food_Times.txt and I don't have access to edit the file on the server atm so I can't examine if theres something wrong with the text file as in blank spaces that might be throwing it off.. When you say in the debugger I can look at each String array as characters... how? – cj1098 Aug 08 '11 at 06:47
  • 1
    Android has a great debugger. If you don't want to use it, you can simply go through every character (get the size via `length()`) and print out the Unicode value via `charAt` to the debug output via `Log.d()`. – EboMike Aug 08 '11 at 06:49
  • @cj1098: You say you've debugged into the code - so presumably you know how to use the debugger. When you examine a string, you should be able to get a more "raw" view on it, which would include access to the underlying char array. – Jon Skeet Aug 08 '11 at 07:23
2
if(temp.equals("Location"))
{
         //your code here
}
does not work

try this
if(temp.contains("Location"))
{
         //your code here
}
Mohit Verma
  • 3,025
  • 1
  • 20
  • 36
0

try like

if(temp.equals("Location")) { //do something }

and

while (!temp.equals("")){
Rasel
  • 15,499
  • 6
  • 40
  • 50
0

if your variable temp is a String, you can also used the method compareTo(String).

if (temp.compareTo("Location") == 0) 
{ 
    //do something 
}
Cedekasme
  • 2,027
  • 1
  • 16
  • 16
0

Try doing this:

if (temp.toLowerCase().compareTo("location") == 0)
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
0

I am doing same scenario , its working fine.

    String result = responsePrimitiveData.toString();
if(!result.equals("0")){ 
     }
Piraba
  • 6,974
  • 17
  • 85
  • 135
0
public String[] getData(){ 
    try {
        int tempGroupCount = 0;
        URL food_url = new URL (Constants.SERVER_DINING);
        BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
        temp = my_buffer.readLine();
        // prime read
        while (temp != null ){
            // check to see if readline equals Location
            Log.w("HERasdfsafdsafdsafE", temp);
            // start a new location
            if (temp.toString().equalsIgnoreCase("Location")
            {
                groups[tempGroupCount] = temp;
                tempGroupCount++;
            }
                Log.w("HERasdfsafdsafdsafE", temp);
                //start for-loop to test to get child info
                //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){
                    //children[groupCount][childrenCount] = temp;
                //}

            temp = my_buffer.readLine();
        }
        my_buffer.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
    }
    return groups;
    }

first try to convert "temp" into string then compare it, apply this may helps you

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
0

you may try the following to find out where your problem is.

final String LOCATION = "Location"; // just to make sure we use the very same character sequence

if (temp.equals(LOCATION)
{
    /* your code here */
}
else
{
    System.out.println("Location : " + Arrays.toString(LOCATION.getBytes(Charset.forName("UTF-8"))));
    System.out.println("temp     : " + Arrays.toString(temp.getBytes(Charset.forName("UTF-8"))));
}

This should print the byte representation of both Strings to standard out. If equals() returns false, the strings differ. Because of unprintable characters or similar looking characters it's sometimes difficult to find the difference. But the byte representation should show you.

(I'm not an android programmer, so I hope the functions exist on android JVM. And sorry for any typos and missing brackets - if any ;-)