Article View: aus.computers.java
Article #782Re: just a quickie
From: Danny Smith
Date: Sun, 16 Apr 2000 00:00
Date: Sun, 16 Apr 2000 00:00
72 lines
2751 bytes
2751 bytes
The reason you can't use == or != on strings is that they're not
primitive types - if you do use == you'll find yourself comparing the
reference that leads to the string, not what's contained within it.
It's like comparing pointers to an object in C or C++.
Of course, if anyone wants to correct me, feel free, I've only been
doing Java for a couple of months.
Danny Smith
Everyone who favors abortion has already been born.
On 02 Apr 2000 19:38:46 +1000, Tim Cross <tcross@tim.northnet.com.au>
wrote:
>Greg,
>
>I think your problem is you need to use the equals() method fo the
>string class - you cannot compare strings with == or !=. Try something
>like
>
>while (!(temp = in.readLine()).equals("")) // while line is not ""
>
>Note that you may have another problem here - what happens when you
>come to the end of the input file? Perhapse you could try something
>like
>
>while ((temp = in.readLine()) != null) {
> if (temp.equals(""))
> // do whatever you do if you get a blank line
> else
> // do whatever you do when you don't get a blank line
>}
>
>Hope this helps
>
>Tim
>"Greg" <gsimon@bigpond.net.au> writes:
>
>> I have to read in a paragraph of text from a text file like the one shown
>> below only the program has to stop reading on the blank line.
>>
>> ================================
>> blah blah blah
>> hello hello
>>
>> start of new paragraph
>> ================================
>>
>> this is what I have tried:
>>
>> BufferedReader in = new BufferedReader(
>> new FileReader("file.txt"));
>> String temp;
>>
>> while((temp = in.readLine()) != "\n") /* I have also tried ...!= "" */
>> {
>> ......
>>
>> could someone please tell me what to compare the String to, or where I am
>> going wrong. any help appreciated.
>>
>> Greg
>>
>>
>
>--
>Tim Cross
>NorthNet Internet Services
>mailto:tcross@northnet.com.au
>phone:+61 2 6772 5973
Technical Details
Message-ID:
<38f9b713.1425432@newsreader.brisnet.org.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<S%dF4.1190$1b4.5128@news-server.bigpond.net.au> <m3d7o86dw9.fsf@tim.northnet.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,1e8f92126f62286c X-Google-Attributes: gid10d6db,public From: smithbcd@hello.star.brisnet.org.au (Danny Smith) Subject: Re: just a quickie Date: 2000/04/16 Message-ID: <38f9b713.1425432@newsreader.brisnet.org.au> X-Deja-AN: 612019246 References: <S%dF4.1190$1b4.5128@news-server.bigpond.net.au> <m3d7o86dw9.fsf@tim.northnet.com.au> X-Original-NNTP-Posting-Host: ts1-226.star.brisnet.org.au X-Complaints-To: abuse@telstra.net X-Trace: nsw.nnrp.telstra.net 955927591 203.37.129.8 (Mon, 17 Apr 2000 09:26:31 EST) Organization: Customer of Telstra Big Pond Direct NNTP-Posting-Date: Mon, 17 Apr 2000 09:26:31 EST Newsgroups: aus.computers.java