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?