Thread View: aus.computers.java
3 messages
3 total messages
Started by Greg
Sat, 01 Apr 2000 00:00
just a quickie
Author: Greg
Date: Sat, 01 Apr 2000 00:00
Date: Sat, 01 Apr 2000 00:00
24 lines
1305 bytes
1305 bytes
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
Re: just a quickie
Author: moz
Date: Sun, 02 Apr 2000 00:00
Date: Sun, 02 Apr 2000 00:00
43 lines
1542 bytes
1542 bytes
Greg wrote:
> 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
Dear Greg,
What I think your problem is, is that you shouldn't be using the operators
:==, != ...
try using:
while(!temp.equals("\n")){
.............
.........
............
}
I forget the exact reason that the !=/== thing doesn't work for strings, I
just remeber it doesn't.
Moz
Re: just a quickie
Author: Tim Cross
Date: Sun, 02 Apr 2000 00:00
Date: Sun, 02 Apr 2000 00:00
56 lines
2169 bytes
2169 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads