0

String line = null;
double value;
BufferedReader in = new BufferedReader(new FileReader("C:\Users\Bilal\Documents\Work\scrambler\input.txt"));

int x=0;
while (line != "end"){


    line = in.readLine();
    System.out.println(line); 

    x++;
}

}}

When I run this.. the loop does not stop when the word end comes.. why???????

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

Always, always, always use equals() for comparison between the texts of Strings. == is only used for checking if a String is null or not.

String s1 = "Hello!";
String s2 = "Hi!";
//s1 might '==' s2, but !s1.equals(s2).

Hope that makes sense!

0

Thnaks for your resonse ..

Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:26)

im getting this erron now??

1

String s1 = "Hello!";
String s2 = "Hi!";
// s1 might '==' s2, but !s1.equals(s2).

No, in that example s1 cannot == s2.
The == tests for s1 referring to exactly the same object as s2, whereas equals tests for two Strings containing the same sequence of letters.
If s1 == s2 then by definition s1.equals(s2) and s2.equals(s1)
But if s1.equals(s2) then it does not imply that s1==s2 because they may refer to two different String objects that just happen to contain identical sequences of letters.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.