Results 1 to 5 of 5
Thread: A string and substring problem
- 07-18-2011, 07:23 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
A string and substring problem
Hi, I wrote a piece of code, which reads from a file line by line. I was trying to determine content of each line by comparing substring to a keyword. However, the comparison at line 79 never returns true (the output command at line 80 was never executed), although the output at line 77 gives exactly the same keyword. I'm confused. Any advice is appreciated.
Output:Java Code:while ( (inputLine = in.readLine()) != null ){ substr = inputLine.trim(); if(substr.length()> 9){ System.out.println(substr.substring(1, 9)); // line 77 if(substr.substring(1, 9)=="nodeName"){ // line 79 System.out.println("node"); // line 80 } } }
Java Code:ar miser nodeName nodeName nodeName nodeName nodeName nodeName nodeName source:1 source:1 source:1 source:2 source:1 source:5 source:6
Last edited by calnastic; 07-18-2011 at 07:41 PM.
- 07-18-2011, 08:07 PM #2
You need to use the equals() method when comparing Strings. The == operator is for primitives.
- 07-18-2011, 08:25 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 11
- Rep Power
- 0
u need to override equals() method in ur class
So that those STRING objects can be compared via equals()
- 07-18-2011, 08:53 PM #4
Why? This is nonsense. There is no need for the OP to write an equals() method.u need to override equals() method in ur class
- 07-18-2011, 09:02 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
How do I remove a substring from a string?
By Unnel in forum New To JavaReplies: 16Last Post: 07-11-2011, 11:56 AM -
Search Substring in String Help Please
By Kestrel01 in forum New To JavaReplies: 3Last Post: 10-26-2010, 06:48 PM -
How to extract substring from a string based on the condition ??
By j_kathiresan in forum Advanced JavaReplies: 5Last Post: 06-22-2010, 09:34 AM -
Reverse a string not using the substring method
By kathyla18 in forum New To JavaReplies: 17Last Post: 04-08-2009, 04:08 AM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks