By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Compare and match readLine() and text in a text file

P: 35
I have a text file, i open the file and try to match the contents of the text file to a text. Here's my code:

FileInputStream input = new FileInputStream ("BinaryFp.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(input));
for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
if(pdtNo=="M5250")
System.out.println("true");
else
System.out.println("false");
}
The problem is, i don't know why the answer is always false. There should be 1 match. Is ther any way that i can compare "M5250" with text in the text file?
Dec 11 '06 #1
Share this Question
Share on Google+
13 Replies


10K+
P: 13,264
I have a text file, i open the file and try to match the contents of the text file to a text. Here's my code:

FileInputStream input = new FileInputStream ("BinaryFp.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(input));
for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
if(pdtNo=="M5250")
System.out.println("true");
else
System.out.println("false");
}
The problem is, i don't know why the answer is always false. There should be 1 match. Is ther any way that i can compare "M5250" with text in the text file?
Always use str.equals(otherString) for comparing strings. Do not use == for that.
Dec 11 '06 #2

P: 35
Always use str.equals(otherString) for comparing strings. Do not use == for that.
I tried using str.equals(otherString), but it doesn't works >_<
Dec 11 '06 #3

10K+
P: 13,264
I tried using str.equals(otherString), but it doesn't works >_<
Post how you have tried it.
Dec 11 '06 #4

P: 35
This is how i use it:

FileInputStream input = new FileInputStream ("BinaryFp.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(input));
for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
if(pdtNo.equals("M5250"))
System.out.println("true");
else
System.out.println("false");
}

But the result is still "false".
Dec 11 '06 #5

10K+
P: 13,264
This is how i use it:

FileInputStream input = new FileInputStream ("BinaryFp.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(input));
for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
if(pdtNo.equals("M5250"))
System.out.println("true");
else
System.out.println("false");
}

But the result is still "false".

Try

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=1;i<801;i++)
  3. {
  4. pdtNo = br.readLine();
  5. System.out.println(pdtNo);
  6. }
  7.  
And see what is being read from the file.
Dec 11 '06 #6

P: 35
Try

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=1;i<801;i++)
  3. {
  4. pdtNo = br.readLine();
  5. System.out.println(pdtNo);
  6. }
  7.  
And see what is being read from the file.
I tried that too, all the text in the text file is printed out correctly, i just cannot match the text in the file to "M5250".
Dec 11 '06 #7

10K+
P: 13,264
I tried that too, all the text in the text file is printed out correctly, i just cannot match the text in the file to "M5250".
When you printed out the lines, was there a line with "M5250" only and no leading spaces or extra spaces after that? The comparison will only match if there is a line that appears exactly as "M5250".

If you are sure such a line exists and with no extra spaces, then you may want to post the textfile as well so we can really see if you are right.
Dec 11 '06 #8

P: 35
When you printed out the lines, was there a line with "M5250" only and no leading spaces or extra spaces after that? The comparison will only match if there is a line that appears exactly as "M5250".

If you are sure such a line exists and with no extra spaces, then you may want to post the textfile as well so we can really see if you are right.
There are spaces. I had done another java file to create that text file (BinaryFp.txt). So now, i'm using java to read this text file i have created. Is there any way which i can remove all the spaces, so that i can compare?
Dec 11 '06 #9

10K+
P: 13,264
There are spaces. I had done another java file to create that text file (BinaryFp.txt). So now, i'm using java to read this text file i have created. Is there any way which i can remove all the spaces, so that i can compare?
Try str.trim();
Dec 11 '06 #10

P: 35
Try str.trim();
I tried "str.trim();", but there are still many spaces when i print it out. And therefore, the result is still false. Here's my code:

for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
String no = pdtNo.trim();
System.out.println(pdtNo);
if(no.equals("M5250"))
System.out.println("true");
else
System.out.println("false");
}
Dec 11 '06 #11

10K+
P: 13,264
I tried "str.trim();", but there are still many spaces when i print it out. And therefore, the result is still false. Here's my code:

for(int i=1;i<801;i++)
{
pdtNo = br.readLine();
String no = pdtNo.trim();
System.out.println(pdtNo);
if(no.equals("M5250"))
System.out.println("true");
else
System.out.println("false");
}
What is the output with

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=1;i<801;i++)
  3. {
  4. pdtNo = br.readLine();
  5. String no = pdtNo.trim();
  6. System.out.println(no);// Print out no instead of pdtNo
  7. }
  8.  
Where are the spaces?
Dec 11 '06 #12

P: 35
What is the output with

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=1;i<801;i++)
  3. {
  4. pdtNo = br.readLine();
  5. String no = pdtNo.trim();
  6. System.out.println(no);// Print out no instead of pdtNo
  7. }
  8.  
Where are the spaces?
Thanx for your help ^-^. It finally works now.
Dec 11 '06 #13

10K+
P: 13,264
Thanx for your help ^-^. It finally works now.
Welcome. Also have a look at http://java.sun.com/j2se/1.4.2/docs/...ng/String.html
Dec 11 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.