473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
13 9069
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
I tried using str.equals(otherString), but it doesn't works >_<
Post how you have tried it.
Dec 11 '06 #4
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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

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

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
5
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split()...
2
by: cricfan | last post by:
I'm parsing a text file to extract word definitions. For example the input text file contains the following content: di.va.gate \'di_--v*-.ga_-t\ vb pas.sim \'pas-*m\ adv : here and there :...
5
by: PyPK | last post by:
I have two files file1 in format <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T F file2 same as file1 <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T T
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
0
by: ruthfish | last post by:
I am used to using VB6 and trying to work in VB.NET as my school have switched to it. I need to compare all the items loading into a combo box to items being read from an array from a text file...
6
by: Justin Fancy | last post by:
Hi Everyone, I'm lookin for a very confusing loop (to me), to compare two files. Here it is. I have two arrays with paths stored in both. example: /en/aviation/you.htm. I need to search array...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
6
by: napatel04 | last post by:
Hi everyone, I would like to know if there is a quick query someone can help me write for the following scenario. I think I can do this with VBA but since this is suppose to be a temp. solution,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.