3

I can't understand why my program not functioning. It compiles but nothing is printed. I have a 5 character word in file. I need to read line from that file and then split it into char array, which I then want print out.Thanks!

import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;

public class test {
    public static void main(String[] args)
    {

        BufferedReader line = null;
        char[] array = new char[7];

        try{

            line = new BufferedReader(new FileReader(args[0]));

            String currentLine;
            while((currentLine = line.readLine()) != null)
            {
                array = currentLine.toCharArray();
            }

            for(int i = 0; i < array.length; i++)
            {
                System.out.print(array[i]);
            }

        }//try

        catch(IOException exception)
        {
            System.err.println(exception);
        }//catch

        finally
        {
            try 
            {
                if(line != null) 
                    line.close();
            }//try

            catch(IOException exception)
            { 
                System.err.println("error!" + exception);
            }//catch

        }//finally
    } // main
} // test
3
  • I think your missing a bracket somewhere in there. Commented Sep 7, 2012 at 19:59
  • This code works fine when given the described input file. please post your input file if Reimeus's answer does not work for you. Commented Sep 7, 2012 at 20:06
  • Tangential to the question at hand, but there's no reason to initialize the array variable with a newly allocated array. It just gets thrown away when the variable is set in the loop. Commented Sep 7, 2012 at 20:26

2 Answers 2

3

Your while loop skips every line except the last one so it could be possible that your last line is empty. To display every line you could have:

while ((currentLine = line.readLine()) != null) {
    array = currentLine.toCharArray();
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i]);
    }
    System.out.println();
}

Or if you just have the 1 line, You could simply use:

String currentLine = line.readLine(); 
...
Sign up to request clarification or add additional context in comments.

Comments

0

Your program prints only last line

You have to Print in loop.

while (....!=null)
{
 array = currentLine.toCharArray();

for(int i = 0; i < array.length; i++)
   {
      System.out.print(array[i]);
   }


}

If above was not a problem than check your file permission.

Check your system may be program is not able to read from file due to permission on file.

1 Comment

wouldn't an exception be thrown if the file permissions were an issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.