0

I'm reading a file line-by-line with Java and parsing the data in each line for a report. Periodically, there will be a URL that's been written to a line, and I want to bypass that line. I need to set up an if...then that tests for the presence of a URL -- in this specific case, I define that as the first four bytes of the line reading http. If the condition is true, read the next line in the file, but if the condition is false, send the current line to the parser for report output.

Simple, no? I'm running into issues where I cannot get my if..then statement to accurately recognize a true condition. My only clue is that when my inputString is equal to the test value, it passes as true. Beyond that...I can't spot anything out of the ordinary.

public class testClass
{
    public static void main(String args[])
    {
        String inputString1 = "http://www.google.com/r";
        String inputString2 = "google";
        String inputString3 = "google search";
        String inputString4 = "http";
        parseInput(inputString1);
        parseInput(inputString2);
        parseInput(inputString3);
        parseInput(inputString4);
    }
    //
    private static void parseInput(String inputString8) {
        int httpFlag;
        if (inputString8.substring(0,4) == "http") 
            { httpFlag = 1467; } else
            { httpFlag = 10644; }
        System.out.println(inputString8+" "+inputString8.toLowerCase().substring(0,4)+" "+httpFlag); 
     } 
}

Here's my output:

http://www.google.com/r http 10644
google goog 10644
google search goog 10644
http http 1467

I am expecting 1467 for both inputString1 and inputString4. The console appears to display my input and substring properly, and nothing in the Java Documentation suggests that my substring would be a char array or anything apart from a string, so I can't figure out why http as the first four bytes of a 4-byte string is "different" from the first four bytes of a (4+x)-byte string. What am I missing?

dwwilson66
  • 6,806
  • 27
  • 72
  • 117

3 Answers3

5

Compare strings with equals, not ==. There's nothing special about the fact the string you're comparing is the result of calling substring.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I believe this is correct. Most of the time == will give you the correct answer, but I've encountered a couple instances when it will not. Its just safer to use equals.. or better yet StringUtils.equals. – BillMan Dec 20 '13 at 19:32
  • @dwwilson66 Why did you choose to use `=` and not `==` as stated? – Sotirios Delimanolis Dec 20 '13 at 19:32
  • Use the method "equals" not the operator "=" – BillMan Dec 20 '13 at 19:33
  • Single = is assignment operator wilson not comparison. – takendarkk Dec 20 '13 at 19:34
  • @Billman "safety" is not involved here. You may want to read the answers to the question this will be closed as a dup of; it works "sometimes" because string literals are pooled (interned / flyweight pattern) and you have two references to the same object, which is what `==` compares. – Brian Roach Dec 20 '13 at 19:37
  • @Brian.. thanks, yes I'm familiar with why it sometimes works, but I thought that if he didn't understand the difference between = and equals, then telling him about interned strings was only going to confuse him more. – BillMan Dec 20 '13 at 19:56
  • 1
    @BillMan: *"Most of the time == will give you the correct answer..."* No, that's not even close to correct. Most of the time, `==` will give you the **wrong** answer. Moreover, the *last* thing you want as an engineer is something that's correct *most* of the time. – T.J. Crowder Dec 20 '13 at 22:13
  • 1
    @BillMan Sorry, but your original comment contradicts your "familiarity". *"Most of the time == will give you the correct answer, but I've encountered a couple instances when it will not"*. You don't "encounter" instances when it's not unless you're doing it wrong. Which is to say, using `==`. `==` does exactly one thing; compares reference values. If you want to know if one `String` contains the same text as another `String` you use `.equals()`. If you want to know if two references point to the same object, you use `==` – Brian Roach Dec 21 '13 at 06:45
  • I agree with you guys. Precise language matters. I did not use it. – BillMan Dec 23 '13 at 18:08
2

use if (inputString8.substring(0,4).equals("http")).

== operator checks if the two expressions are the same object, you want to check if they have the same value.

klarki
  • 915
  • 4
  • 13
1

== tests to see if two things are the same object. If you want to compare strings char by char use the equals () method of the String class.

takendarkk
  • 3,347
  • 8
  • 25
  • 37