[Bluej-discuss] java.io

Davin McCall davmac at bluej.org
Tue Jul 11 00:34:12 BST 2006


Hi Luis, the problem is your while loop:


                        while((c = in.read()) != -1)

                        {

                            System.out.println(in.readLine());

                        }


The "in.read()" gets evaluated each iteration of the loop, and it reads 
the first character of the line. The "readLine()" method when called 
reads the rest of the line, excluding the first character which has 
already been read.

It seems like you're essentially using the "in.read()" method to detect 
the end-of-file condition. However, readLine() can indicate this 
condition also (it returns null). You could fix your program by getting 
rid of of the variable c (replacing it with a String variable), and 
moving the readLine() call inside the conditional. I'll leave the exact 
solution as an exercise :-)

In regards to your other question, it is quite common to leave out 
constructors. In this case a default constructor (which essentially does 
nothing) will be supplied for you by the compiler. However, as a matter 
of style I consider it better to include a constructor, even it is 
empty. (As a general rule, it's better to make behavior explicit).

(The default constructor doesn't really do nothing - it calls the 
superclass constructor. But if you don't understand this for now, don't 
worry about it too much; in your own classes, if you leave out the 
constructor, you will generally get a constructor which effectively does 
nothing).

Davin


Luis Moreira wrote:
>
> Hi Guys,
>
> I have written a small class based partially on the example given on 
> the book and partially based on the Sun Tutorial. This class will read 
> a text file and print it on the terminal window.
>
> This is the code:
>
> import java.io.*;
>
>  
>
> /**
>
>  * Reads a text file and prints it to text interface.
>
>  *
>
>  * @author (Luis moreira)
>
>  * @version (10/07/06)
>
>  */
>
> public class TextPrint
>
> {
>
>             // instance variables
>
>            
>
>  
>
>             /**
>
>              * Constructor for objects of class TextPrint
>
>              */
>
>             public TextPrint()
>
>             {
>
>                        
>
>             }
>
>  
>
>             /**
>
>              * main method
>
>              *
>
>              * @param  none
>
>              * @return none
>
>              */
>
>             public static void main(String[] args) throws IOException
>
>             {
>
>                         File inputFile = new File("test.txt");
>
>                         BufferedReader in = new BufferedReader(new 
> FileReader(inputFile));
>
>                         int c;
>
>                         while((c = in.read()) != -1)
>
>                         {
>
>                             System.out.println(in.readLine());
>
>                         }
>
>             }
>
> }
>
>  
>
> When I run the class the first character of each line is missing when 
> printed on the window, but the file is still ok. Why is this happening?
>
>  
>
> The other question is, in other classes I have seen like the example 
> ones in the Sun Java tutorial, the Constructor is missing, but yet the 
> class compiles and runs ok. Is this a normal practice?
>
> Thanks for the help.
>
> Best regards
>
>                         Luis
>
>  
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060711/67a9b14a/attachment.html


More information about the bluej-discuss mailing list