Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default if statement with strings not working... again

    hey, this morning I learned that when comparing strings through an if statement, always use .equals. However, for whatever reason, this if statement just isn't working.


    Java Code:
    pName = "word";
    line = "its:word";
    String delims = "[:]+";
    String[] tokens = line.split(delims);
    List<String> wordList = Arrays.asList(tokens);
    if ((wordList.get(0)).equals(pName)){
    	username = wordList.get(1);	
    }
    but it does work if I write out the word like so:

    Java Code:
    pName = "word";
    line = "its:word";
    String delims = "[:]+";
    String[] tokens = line.split(delims);
    List<String> wordList = Arrays.asList(tokens);
    if ((wordList.get(0)).equals("word")){
    	username = wordList.get(1);	
    }
    any ideas?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    You may want to check your code listing above, because while you say the second works, it should not. However,
    Java Code:
    wordList.get(0)).equals("word")//false
    wordList.get(1)).equals("word")//true

  3. #3
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    sorry about that those are just place fillers, it should say
    (wordList.get(0)).equals("its")

  4. #4
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    why don't you put in some print statements and show the result.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I didn't do much other than adding one extra String variable for test purpose in below code,
    Java Code:
                    sName = "its";
    	        pName = "word";
    		line = "its:word";
    		String delims = "[:]+";
    		String[] tokens = line.split(delims);
    		List<String> wordList = Arrays.asList(tokens);
    		if ((wordList.get(1)).equals(tokens[1])) {
    			System.out.println("Success");
    		}
    All the condition mentioned below returned true for me,
    Java Code:
    if ((wordList.get(0)).equals(sName))
    if ((wordList.get(0)).equals("its"))
    if ((wordList.get(1)).equals(pName))
    if ((wordList.get(1)).equals("word"))
    if ((wordList.get(0)).equals(tokens[0]))
    if ((wordList.get(1)).equals(tokens[1]))
    I highly suspect that OP might be evaluating wrong values with wrong index, just like the one posted in his original code,
    Java Code:
    if ((wordList.get(0)).equals("word"))
    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  2. working with unicode strings
    By drift in forum New To Java
    Replies: 4
    Last Post: 10-08-2010, 07:58 AM
  3. Update statement not working.
    By OMFGITSROHIT in forum JDBC
    Replies: 5
    Last Post: 04-08-2010, 01:03 PM
  4. Replies: 2
    Last Post: 12-06-2008, 06:54 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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