Author |
Message |
person
|
Posted: Sun Oct 08, 2006 11:31 am Post subject: file io |
|
|
the code i have below only reads every other line
it there any way for me to make it read every line in a file?
code: |
import java.io.*;
public class Test
{
public static void main (String [] args) throws IOException
{
FileReader read = new FileReader ("file.txt");
BufferedReader in = new BufferedReader (read);
do
{
System.out.println (in.readLine ());
}while (in.readLine () != null);
in.close ();
}
}
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Sun Oct 08, 2006 12:31 pm Post subject: (No subject) |
|
|
code: |
System.out.println (in.readLine ());
}while (in.readLine () != null);
|
you're in.readLine twice, but output just once. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
CroAnte
|
Posted: Fri Oct 13, 2006 10:07 am Post subject: Re: file io |
|
|
person wrote: the code i have below only reads every other line
it there any way for me to make it read every line in a file?
code: |
import java.io.*;
public class Test
{
public static void main (String [] args) throws IOException
{
FileReader read = new FileReader ("file.txt");
BufferedReader in = new BufferedReader (read);
do
{
System.out.println (in.readLine ());
}while (in.readLine () != null);
in.close ();
}
}
|
Change your do structure to:
code: |
do
{
String text=in.readLine();
System.out.println (text);
}while (text != null);
|
|
|
|
|
|
 |
wtd
|
Posted: Fri Oct 13, 2006 11:19 am Post subject: Re: file io |
|
|
CroAnte wrote: Change your do structure to:
code: |
do
{
String text=in.readLine();
System.out.println (text);
}while (text != null);
|
Close, but not quite.
You see, the "text" variable is scoped locally to the block in this case. Therefore the test, which is outside the block, is unaware of its existence. |
|
|
|
|
 |
zylum

|
Posted: Fri Oct 13, 2006 6:55 pm Post subject: (No subject) |
|
|
i think while is more appropriate than do while:
code: | try {
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
String str;
while ((str = in.readLine()) != null) {
//do stuff
}
in.close();
} catch (IOException e) {
} |
|
|
|
|
|
 |
HellblazerX

|
Posted: Sun Oct 15, 2006 2:15 pm Post subject: (No subject) |
|
|
code: | while ((str = in.readLine()) != null) {
//do stuff
} |
I'm not too sure that would work. You're trying to compare a statement (or is it an expression) with a value, which you can't do. If you're going to have while loops, you'll need to initialize the str variable before the loop:
code: | String str = in.readLine ();
while (str != null) {
//do stuff
str = in.readLine () // Get next line
} |
|
|
|
|
|
 |
[Gandalf]

|
Posted: Sun Oct 15, 2006 6:09 pm Post subject: (No subject) |
|
|
Ah, but what if statements return a value too? Try it.
code: | int num;
if ((num = 5) == 5)
System.out.println("Yep"); |
Or more obviously:
code: | int num;
System.out.println(num = 5); |
|
|
|
|
|
 |
wtd
|
Posted: Sun Oct 15, 2006 6:17 pm Post subject: (No subject) |
|
|
Yes, C-family languages pretty much erase the barrier between statement and expression. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
CroAnte
|
Posted: Wed Oct 18, 2006 9:15 am Post subject: Re: file io |
|
|
wtd wrote: CroAnte wrote: Change your do structure to:
code: |
do
{
String text=in.readLine();
System.out.println (text);
}while (text != null);
|
Close, but not quite.
You see, the "text" variable is scoped locally to the block in this case. Therefore the test, which is outside the block, is unaware of its existence.
You're right. In my defense, it was Friday.
The other option is to declare the text variable earlier in the code. Our teacher gets us to do it at the beginning of the main method.... |
|
|
|
|
 |
Tony

|
Posted: Wed Oct 18, 2006 10:00 am Post subject: Re: file io |
|
|
CroAnte wrote: The other option is to declare the text variable earlier in the code. Our teacher gets us to do it at the beginning of the main method....
A variable should only have as much scope as it needs. I hope your teacher isn't telling you to declare all your variables as method wide, just because it appears easier.
It's best you understand why you're doing something a certain way or another, and don't dismiss "just do it this way" lightly. You'll miss out, and quite possibly re-enforce bad practice from continues use, which is arguably more difficult to overcome than to learn a new concept from scratch. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|