
-----------------------------------
person
Sun Oct 08, 2006 11:31 am

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?


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 ();
  } 
}


-----------------------------------
Tony
Sun Oct 08, 2006 12:31 pm


-----------------------------------

  System.out.println (in.readLine ());
    }while (in.readLine () != null); 

you're in.readLine twice, but output just once.

-----------------------------------
CroAnte
Fri Oct 13, 2006 10:07 am

Re: 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?


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:


    do
    {
      String text=in.readLine();
      System.out.println (text);
    }while (text != null);


-----------------------------------
wtd
Fri Oct 13, 2006 11:19 am

Re: file io
-----------------------------------
Change your do structure to:


    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
Fri Oct 13, 2006 6:55 pm


-----------------------------------
i think while is more appropriate than do while:

try {
    BufferedReader in = new BufferedReader(new FileReader("file.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        //do stuff
    }
    in.close();
} catch (IOException e) {
}

-----------------------------------
HellblazerX
Sun Oct 15, 2006 2:15 pm


-----------------------------------
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:
String str = in.readLine ();
while (str != null) {
     //do stuff
     str = in.readLine () // Get next line
}

-----------------------------------
[Gandalf]
Sun Oct 15, 2006 6:09 pm


-----------------------------------
Ah, but what if statements return a value too?  Try it.
int num;
if ((num = 5) == 5)
    System.out.println("Yep");
Or more obviously:
int num;
System.out.println(num = 5);

-----------------------------------
wtd
Sun Oct 15, 2006 6:17 pm


-----------------------------------
Yes, C-family languages pretty much erase the barrier between statement and expression.

-----------------------------------
CroAnte
Wed Oct 18, 2006 9:15 am

Re: file io
-----------------------------------
Change your do structure to:


    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
Wed Oct 18, 2006 10:00 am

Re: file io
-----------------------------------
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.
