how compare two string?

E

Elbrethril

Hello, I must find a method that verification if two strings are equal
or not. The text comes extracted from a file and there are some empty
lines. I have tried with the method equals(), and also compareTo() of
the class String, but they return java.lang.NullPointerException when
they find the line empty. How I make to arrive at the end of the rows?

Thanks

P.S. Sorry for my English
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Elbrethril schreef:
Hello, I must find a method that verification if two strings are equal
or not. The text comes extracted from a file and there are some empty
lines. I have tried with the method equals(), and also compareTo() of
the class String, but they return java.lang.NullPointerException when
they find the line empty. How I make to arrive at the end of the rows?

equals() is the right method, but you’ll have to do a not-null check
first, to see whether the object you’re invoking it on is not null.
However, I would expect empty lines to return "", not null. Can you
post some code?

Also look for the idiom "some string".equals(varString);

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFE1veke+7xMGD3itQRAs40AJ9F5IKZ/VrsKDChXTq7kZaufOolXgCaAjZ3
ONcMHhwHtsulZIGO4S0o+J8=
=fv8C
-----END PGP SIGNATURE-----
 
E

Elbrethril

equals() is the right method, but you'll have to do a not-null check
first, to see whether the object you're invoking it on is not null.
However, I would expect empty lines to return "", not null. Can you
post some code?

Also look for the idiom "some string".equals(varString);

ok, but the object is not null, because until it does not find the null
line returns the right string.

String line = "";
BufferedReader in = null;
in = new BufferedReader(new FileReader("DatasetPiccolo.txt"));
String key = null;
while (line != null)
String readed = getTag(line);

if (readed == "authors")
{
key = getValue(line);
System.out.println(authors);
}

getTag is a method for delete some characters from the string. getValue
is for extract the text that I want.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Elbrethril schreef:
ok, but the object is not null, because until it does not find the null
line returns the right string.

String line = "";
BufferedReader in = null;
in = new BufferedReader(new FileReader("DatasetPiccolo.txt"));
String key = null;
while (line != null)
String readed = getTag(line);

if (readed == "authors") "authors".equals(readed)
{
key = getValue(line);
System.out.println(authors);
}

as I suggested.

H.

P.S. ‘readed’ is very odd English. It is ‘read’, pronounced like the
colour.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFE1v1Ue+7xMGD3itQRAtayAJ0U/yerLo3gVovgYrDT/On0jBmzQQCeLAbu
BE9MrUOhS66YmmFyMdCJhiQ=
=f5BK
-----END PGP SIGNATURE-----
 
E

Elbrethril

Hendrik Maryns ha scritto:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Elbrethril schreef:

as I suggested.

H.

P.S. 'readed' is very odd English. It is 'read', pronounced like the
colour.
- --
Hendrik Maryns

thanks!
 
G

Gordon Beaton

ok, but the object is not null, because until it does not find the null
line returns the right string.

String line = "";
BufferedReader in = null;
in = new BufferedReader(new FileReader("DatasetPiccolo.txt"));
String key = null;
while (line != null)
String readed = getTag(line);

if (readed == "authors")
{
key = getValue(line);
System.out.println(authors);
}

getTag is a method for delete some characters from the string. getValue
is for extract the text that I want.

In addition to the String comparison issue, the posted example fails
to actually read from the file, so you will never find what you are
looking for.

In order to read the file one like at a time, the loop should look
like this:

while ((line = in.readLine()) != null) {
// process line
...
}

/gordon
 
O

Oliver Wong

Elbrethril said:
ok, but the object is not null, because until it does not find the null
line returns the right string.

String line = "";
BufferedReader in = null;
in = new BufferedReader(new FileReader("DatasetPiccolo.txt"));
String key = null;
while (line != null)
String readed = getTag(line);

The program will loop forever at this point, because line will never be
null.

[rest of program snipped]

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top