I have a problem outputting the contents of a text file. Whenever I try to display the contents of a file a "null" word is printed at the end of the output.

Any help would be deeply appreciated.

import java.io.*;
import java.lang.*;

public class prog1
{
	public static void main(String[] args)
	{
		readFile();
	}	//end of main
	
	public static boolean isDoubleString(String s)
    {
       try
       {
			Double.parseDouble(s);
			return true;
		}
		
		catch(NumberFormatException nx)
		{
			return false;
		}
    }   // end of isDoubleString()
	
	public static String kbdInput(String prompt)
	{
		String s = "";
		System.out.print(prompt);
		
		try
		{
			BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
			s = kbd.readLine();
		}
		
		catch(IOException iox)
		{
			System.out.println("No keyboard detected...");
			System.exit(0);
		}
		
		return s;
	}	//end of kbdInput
	
	public static void readFile()
	{
		String fname = kbdInput("Enter file name: ");
		BufferedReader infile = null;
		
		try
		{
			infile = new BufferedReader(new FileReader(fname));
		}
		catch(IOException iox)
		{
			System.out.println("File does not exist...");
			System.exit(0);
		}
		
		String aLine = "";
			
		try
		{
			while(aLine != null)
			{
				aLine = infile.readLine();
				System.out.println(aLine);
			}
			infile.close();
		}
		catch(IOException iox)
		{
			System.out.println("File is empty...");
			System.exit(0);
		}
	} //end of readFile
}
aLine = infile.readLine();
System.out.println(aLine);  // Print the last line read, including the null at EOF

how do I fix this problem?
I just started learning java and do not have any experience in it.

This seemed to fix the problem

while((aLine = infile.readLine()) != null)
			{
				System.out.println(aLine);
			}

but I do not know why

To see what is happening, you need to play computer. Step thru the code statements one at a time, keeping track of the values of the variables as you go.

while((aLine = infile.readLine()) != null)

What does this statement do? It will execute the body in {} if ... otherwise it won't

Remember, order of operations matters. You want to check first, not read in input first.

Actually I find that kind code confusing and I never use it. This is simpler:

// get the first line of the file. If it is not null go in the loop
String aLine = infile.readLine();
while (aLine != null) {

  // the line is not null. So you can do whatever you want to process it
  System.out.println(aLine);

..........

  // at the end of the loop. At the LAST command, read the next line:
  aLine = infile.readLine();
  // if the next line is null, then the loop will exit and you finish reading the file.
  // that is why it must be at the end.
}

At the last command you read the next line, then you go at the top of the loop. If that line is null then there is no line, and you don't continue looping.

Another, more stupid way, is this. Though I don't recommend it:

while (true) {
  String aLine = infile.readLine();
  if (aLine==null) {
     System.out.println("No more lines. Will exit the loop");
     break;
  }
  // no else needed here because if you go in the above if, you will break from the loop and nothing else will execute.
  // with the break command you exit the loop

  System.out.println("Line:"+aLine);
}
This article has been dead for over six months. Start a new discussion instead.