0

I have a text file and each of its line is like that

author-title-kind

I have a Java program parsing this file and it must returns only the books whose author is "example". I read a line at a time, and then I split the string with StringTokeneizer or split(). So I will get 3 items: author, title, kind. Then I check if the first item string is equal to "example". The problem is that I always get false, and never true. Is there any hidden character so that this comparison ends always with false? Maybe I should check with "example-", or "-example"...or anything else?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 2
    Can you show us your code? – Rohit Jain Feb 08 '13 at 17:42
  • 4
    Are you comparing strings with `==` or with `"example".equals(yourData)`? If with `==` then [this question](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) should interest you. – Pshemo Feb 08 '13 at 17:43
  • 1
    Also, did you try to print individual string variables after split to see how they are split exactly? – AC1 Feb 08 '13 at 17:47
  • 1
    If you are developing on an IDE like Eclipse, Netbeans, or Intellij, try putting some breakpoints and debugging to see what values your tokens contain. – Sotirios Delimanolis Feb 08 '13 at 17:49
  • Yes, I am using Eclipse and I have tried all you've said. I print in terminal item[1], the author, and "example" and I see they appear totally equal. That's why I was wondering about an hidden char – SagittariusA Feb 08 '13 at 17:52
  • Could you [[edit]] your question and add code which shows how you compare these values? – Pshemo Feb 08 '13 at 17:59
  • 1
    I've just solved. As suggested by you, it is necessary to do string1.equals(string2) to verify the two string are equal. Instead I used =...still so bad influenced by python – SagittariusA Feb 08 '13 at 18:55

1 Answers1

1

Remember that String.split() takes a regular expression as a separator and not just a string. I would use apache commons StringUtils.split() if you want basic string splitting with a simple string.

Eurig Jones
  • 8,226
  • 7
  • 52
  • 74
  • How will that affect the splitting in this case. OP just needs to split on `-`, which is only special when used in a character class, that too between two characters. – Rohit Jain Feb 08 '13 at 17:48
  • True. I guess the OP needs to show his code and text file as well. – Eurig Jones Feb 08 '13 at 17:49