I'm having problems making a class that can read and print from a file. It seems like the file name passed to the constructor isn't actually being assigned to the fileName variable, or maybe I'm doing something wrong with the File and Scanner objects. I really don't know what's wrong or how to fix it. I'm a beginner and just covered using files in my class, so I'm probably missing something obvious. Thanks for any help anyone can give :)
Here's all my code and the instructions for the assignment below.
The assignment was to:
Write a class named FileDisplay with the following methods:
constructor: accepts file name as argument
displayHead: This method should display only the first five lines of the file’s contents. If the file contains less than five lines, it should display the file’s entire contents.
displayContents: This method should display the entire contents of the file, the name of which was passed to the constructor.
displayWithLineNumbers: This method should display the contents of the file, the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 1.
My code:
import java.io.*;
import java.util.Scanner;
public class FileDisplay {
// just using little random .txt files to test it
private String fileName = "example1.txt";
public FileDisplay(String fileName) throws IOException {
this.fileName = fileName;
}
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
// displays first 5 lines of file
public void displayHead() {
for (int x = 0; x < 5 && inputFile.hasNext(); x++) {
System.out.println(" " + inputFile.nextLine());
}
}
//displays whole file
public void displayContents() {
while (inputFile.hasNext()) {
System.out.println(" " + inputFile.nextLine());
}
}
// displays whole file with line numbers
public void displayWithLineNumbers() {
while (inputFile.hasNext()) {
int x = 1;
System.out.println(x + ": " + inputFile.nextLine());
x++;
}
}
@Override
public String toString() {
return "FileDisplay [someFile=" + fileName + "]";
}
}
I also wrote a driver application to test if the class was working or not:
import java.io.*;
public class FileDisplayTest {
public static void main(String[] args) throws IOException {
PrintWriter ex1 = new PrintWriter("example1.txt");
ex1.println("apple");
ex1.println("pear");
ex1.println("grape");
ex1.close();
FileDisplay test = new FileDisplay("example1.txt");
test.displayContents();
System.out.println(test.toString());
}
}