Reading from a text file and printing the output on screen


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Reading from a text file and printing the output on screen

  1. #1
    Join Date
    May 2004
    Posts
    5

    Reading from a text file and printing the output on screen

    Hello
    I'm new to java and I need I have wriiten a java code to read the contents of a .txt file (viewcx.txt), but each time i try to compile the code i always get these error messages:

    h:\>javac Viewcx.java
    Viewcx.java:14: not a statement
    for ((line = bufread.readLine()) != null && !line.equals ("")); {
    ^
    Viewcx.java:14: ';' expected
    for ((line = bufread.readLine()) != null && !line.equals ("")); {
    ^
    Viewcx.java:14: illegal start of expression
    for ((line = bufread.readLine()) != null && !line.equals ("")); {
    ^
    Viewcx.java:19: illegal start of expression
    return;
    ^
    Viewcx.java:19: ')' expected
    return;
    ^
    5 errors

    I have looked through the codes several times but can't find any error there. Please help. te code is below

    //instruct java to get ready to use any class in the input output package
    //on to java 2 S. Narasimhan
    import java.io.*;
    //defines new class that is publicly accessible
    public class Viewcx {
    public static void cx() throws IOException
    {

    FileInputStream opennewcx = new FileInputStream("newcx.txt");
    InputStreamReader reader = new InputStreamReader(opennewcx);
    BufferedReader bufread = new BufferedReader(reader);
    String line;

    for ((line = bufread.readLine()) != null && !line.equals ("")); {
    System.out.println ("Your details " + line);
    }

    opennewcx.close();
    return;
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    you're using the for loop wrong. For loops have three parts. The first part is normally used to initialise a variable, the second part is a boolean expression, and the third part is normally used to increment a variable. Each part needs to be seperated by a semi-colon. It's perfectly ok to not put something in one of the parts, but you still need the semi-colons. If you rewrite the for loop to look like this
    Code:
    for(line = bufread.readLine(); line != null && !line.equals(""); line = bufread.readLine())
    {
        //in your code you ended the above loop with a semicolon, don't do that.
        //If you do that then this code will only ever run once
        //because it would in no way be connected to the for loop.
    }
    When the loop starts it does the first statement, thats the only time that statement ever gets run. Then it does the boolean expression. If the boolean expression is true, it runs the loop body. When it reaches the end of the body it runs the last expression in the loop. It then runs the boolean expression again, and starts over. It only exits the loop when the boolean is false.

    Also, take out the return from the end of the method. It's a void method, it doesn't need a return statement. If you want to end the method prematurely you can use return, but since it's the last line of the method it isn't needed. It returns automatically.

    Make the changes and see if that gets rid of all the compile errors. It should do, because all those errors were probably caused by the incorrect syntax in the for loop, and correcting it will get rid of them all. If not, just come back and we'll have another go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center