3

So I'm reading input from a file, which has say these lines:

       NEO
You're the Oracle?
       NEO
Yeah.

So I want to output his actual lines only, not where it says NEO. So I tried this:

if(line.trim()=="NEO")
    output=false;
   if (output)
    TextIO.putln(name + ":" + "\"" + line.trim() + "\""); // Only print the line if 'output' is true

But thats not working out. It still prints NEO. How can I do this?

bitmoe
  • 391
  • 2
  • 5
  • 13

5 Answers5

7

When comparing strings in Java you have to use the equals() method. Here's why.

if ( "NEO".equals(line.trim() )
Community
  • 1
  • 1
Sean
  • 7,597
  • 1
  • 24
  • 26
5

I think you're looking for line.trim().equals("NEO") instead of line.trim() == "NEO"

That said, you can get rid of the output variable by instead doing

if(!line.trim().equals("NEO"))
{
    TextIO.putln(name + ":" + "\"" + line.trim() + "\""); // Only print the if it isn't "NEO"
}
Dave McClelland
  • 3,385
  • 1
  • 29
  • 44
  • @mohabitar Check up latest revision if you haven't, I suggest a shorter way to do it that may make more sense to you. Also, it's very helpful to know the difference between `==` and `.equals()`. Check out the links in zigdon's and gameaddict's posts for information about .equals – Dave McClelland Sep 15 '10 at 02:42
  • I used this, but now its ignoring all lines in the text file, not printing anything. Do you have enough to know whats wrong or would you need the rest of the code? – bitmoe Sep 15 '10 at 02:49
  • @Mohabitar It's strange that it isn't working. I don't think seeing anymore of the code would help - I have a pretty good idea of what it's doing. If you can't get it to work my way, then just use the code you originally had with .equals instead of ==. My suggestion was a nitpick more than requirement. On an unrelated note, on StackOverflow, it's useful to include an @ sign and part of the name of the person you're directing a comment toward - they'll get a useful notification letting them know to look at the thread. You should have gotten such an alert from this comment. – Dave McClelland Sep 15 '10 at 03:17
4

Strings are objects in Java. This means you can't just use the == operator to compare them, since the two objects will be different even if they both represent the same string. That's why the String object implements an equal() method, which will compare the contents of the objects, instead of just their memory addresses.

Reference

zigdon
  • 14,573
  • 6
  • 35
  • 54
2

In Java, Strings are objects. And the == operator checks for exact equality.

In other terms

final String ans = line.trim();
final String neo = "NEO";
if (ans == neo)  ... 

implies you want to check that the ans and the neo objects are the same. They are not, since Java allocated (instantiated) two objects.

As other said, you have to test for equality using a method created for the String object, that actually, internally, checks the values are the same.

if (ans.equals(neo)) ...
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
1

try the following:

if(line.trim().equals("NEO"))
Ibrahim
  • 1,247
  • 3
  • 13
  • 21