Results 1 to 5 of 5
  1. #1
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default 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.

    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				
    					}
    				}
    				
    			}
    Output:
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You need to use the equals() method when comparing Strings. The == operator is for primitives.

  3. #3
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    u need to override equals() method in ur class

    So that those STRING objects can be compared via equals()

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    u need to override equals() method in ur class
    Why? This is nonsense. There is no need for the OP to write an equals() method.

  5. #5
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

Similar Threads

  1. How do I remove a substring from a string?
    By Unnel in forum New To Java
    Replies: 16
    Last Post: 07-11-2011, 11:56 AM
  2. Search Substring in String Help Please
    By Kestrel01 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 06:48 PM
  3. Replies: 5
    Last Post: 06-22-2010, 09:34 AM
  4. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  5. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM

Posting Permissions

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