Results 1 to 5 of 5
- 07-11-2012, 12:13 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 14
- Rep Power
- 0
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.
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..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); }Last edited by yasarqamar; 07-11-2012 at 12:20 PM. Reason: added [code] ... [/code] tags
- 07-11-2012, 12:20 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2012, 12:35 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 14
- Rep Power
- 0
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++.
- 07-11-2012, 12:36 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 14
- Rep Power
- 0
Re: Program fails to compare a String Token with a String.
Thanks Joe. Ur help is appreciated.
- 07-11-2012, 12:42 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Program fails to compare a String Token with a String.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
how to compare one string with another dynamic length pattern string?
By ravi.josih53 in forum New To JavaReplies: 11Last Post: 08-15-2011, 10:12 AM -
Why we use String compare to String Buffer
By Sharath_Forums in forum New To JavaReplies: 1Last Post: 12-06-2010, 06:21 AM -
spliting a string and checking each token's format
By Implode in forum New To JavaReplies: 1Last Post: 10-18-2009, 08:41 PM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM -
Help with string token
By trill in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 05:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks