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?
13 9069
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.
Always use str.equals(otherString) for comparing strings. Do not use == for that.
I tried using str.equals(otherString), but it doesn't works >_<
I tried using str.equals(otherString), but it doesn't works >_<
Post how you have tried it.
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".
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 -
-
for(int i=1;i<801;i++)
-
{
-
pdtNo = br.readLine();
-
System.out.println(pdtNo);
-
}
-
And see what is being read from the file.
Try -
-
for(int i=1;i<801;i++)
-
{
-
pdtNo = br.readLine();
-
System.out.println(pdtNo);
-
}
-
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".
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.
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?
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();
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");
}
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 -
-
for(int i=1;i<801;i++)
-
{
-
pdtNo = br.readLine();
-
String no = pdtNo.trim();
-
System.out.println(no);// Print out no instead of pdtNo
-
}
-
Where are the spaces?
What is the output with -
-
for(int i=1;i<801;i++)
-
{
-
pdtNo = br.readLine();
-
String no = pdtNo.trim();
-
System.out.println(no);// Print out no instead of pdtNo
-
}
-
Where are the spaces?
Thanx for your help ^-^. It finally works now.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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()...
|
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 :...
|
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
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |