Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

how compare two string?

Thread view: 
Elbrethril - 07 Aug 2006 08:59 GMT
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
Hendrik Maryns - 07 Aug 2006 09:19 GMT
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
Elbrethril - 07 Aug 2006 09:38 GMT
> 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.
Hendrik Maryns - 07 Aug 2006 09:44 GMT
Elbrethril schreef:
>> 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.
[quoted text clipped - 14 lines]
>
>             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
Elbrethril - 07 Aug 2006 09:57 GMT
Hendrik Maryns ha scritto:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 32 lines]
> - --
> Hendrik Maryns

thanks!
Norman D. - 07 Aug 2006 13:58 GMT
I didn't realize 'read' was a colour ;-)

--Norman

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 44 lines]
> =f5BK
> -----END PGP SIGNATURE-----
Luke Webber - 08 Aug 2006 02:19 GMT
> I didn't realize 'read' was a colour ;-)

Certainly it is...

Q: What's black and white and read all over?

A: A newspaper.

Luke
Norman D. - 08 Aug 2006 13:19 GMT
I stand corrected!

--Norman

> > I didn't realize 'read' was a colour ;-)
>
[quoted text clipped - 5 lines]
>
> Luke
Gordon Beaton - 07 Aug 2006 10:08 GMT
> ok, but the object is not null, because until it does not find the null
> line returns the right string.
[quoted text clipped - 14 lines]
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Oliver Wong - 07 Aug 2006 20:03 GMT
>> 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.
[quoted text clipped - 12 lines]
>        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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.