Results 1 to 5 of 5
  1. #1
    yasarqamar is offline Member
    Join Date
    Jul 2012
    Posts
    14
    Rep Power
    0

    Default Program fails to compare a String Token with a String.

    I'll just post a portion of my code that I have a question about.

    Java Code:
    String strLine = null;
      while ((strLine = br.readLine()) != null)
      {
          String outputLine1 = "";
          String key = "";
          String outputLine2 = "";
          StringTokenizer st = new StringTokenizer(strLine, "<>", true);//System.out.println("yasar");
          while(st.hasMoreTokens()) 
          {
              key = st.nextToken();System.out.println(key);
              //String val = st.nextToken();
              if(key == "<"){
                  outputLine1 += key;
                  outputLine2 += key;
                  String val = st.nextToken();
                  for ( int i = 0; i < val.length(); ++i )
                      {
                      char c = strLine.charAt(i);
                      int j = (int) c;
                      int k = j+1;
                      char d = (char) k;
                      outputLine1 += c;
                      outputLine2 += d;
                      }
              }
              else if(key == ">"){
                  outputLine1 += key;
                  outputLine2 += key;
              }
          }
          output.write(outputLine2);
          //System.out.println(outputLine1);
          System.out.println(outputLine2);
      }
    It fails to compare the token named 'key' in the if-else if condition inside the nested while. As a result, code inside this if-else branch is never executed. Can anybody let me know why is that so? Maybe its something trivial that i'm missing..
    Last edited by yasarqamar; 07-11-2012 at 12:20 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Program fails to compare a String Token with a String.

    Don't compare two Strings for equality with the == operator; use the equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    yasarqamar is offline Member
    Join Date
    Jul 2012
    Posts
    14
    Rep Power
    0

    Default Re: Program fails to compare a String Token with a String.

    Okay i figured out my problem. Strings (which are objects) can not be compared like primitive data types. I thought they can be compared the same way as i was doing in C++.

  4. #4
    yasarqamar is offline Member
    Join Date
    Jul 2012
    Posts
    14
    Rep Power
    0

    Default Re: Program fails to compare a String Token with a String.

    Thanks Joe. Ur help is appreciated.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Program fails to compare a String Token with a String.

    Quote Originally Posted by yasarqamar View Post
    Okay i figured out my problem. Strings (which are objects) can not be compared like primitive data types. I thought they can be compared the same way as i was doing in C++.
    You can only do that in C++ because the == operator is overloaded for the 'string' type objects. Java doesn't have full blown operator overloading (praise the lord for that) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  3. Why we use String compare to String Buffer
    By Sharath_Forums in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 06:21 AM
  4. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 PM
  5. Help with string token
    By trill in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •