Jake Rasko Jake Rasko - 1 year ago 64
Java Question

Load Text File Into Object in Java

I am trying to write a class but running into problems. The problem I am having is I need to read in a text file and return it back out of the method. I am stuck with many errors. Could anyone help me with this?

The text file looks like this

Question 1

Answer 1

Question 2

Answer 2

Question 3

Answer 3



import java.io.*;
import java.util.*;

public class Quiz {

private String fName; // Name of the file you will be reading from
private Scanner theFile; // Scanner use to read from the file
public int status = 0;
public String line = null;
public int i = 0;

// Create a new Quiz object by opening the associated file. Note that this
// method throws an IOException, so in the method that calls it you must
// also
// have in the header "throws IOException". We will discuss how to handle
// these exceptions later.
public Quiz(String f) throws IOException {

File Quiz = new File(f);
Scanner theFile = new Scanner(Quiz);
return Quiz;

// First check the status. If it is 1 or 2 simply return false.
// If it is 0, check the file:
// If there is a line in the file, return true
// If there is no line in the file, set status to 1 and
// return false.
public boolean hasAQuestion() {
if (status == 1 || status == 2) {
return false;
else if (status == 0) {
if (line = theFile.readLine() != null) {
return true;
else if (line = theFile.readLine() == null) {
status = 1;
return false;

// Return that status of the Quiz object:
// Status = 0: everything ok
// Status = 1: end of file reached
// Status = 2: error has occurred
public int getStatus() {
if (line = theFile.readLine() != null) {
status = 0;
else if (line = theFile.readLine() == null) {
status = 1;
else if (line.theFile.readLine() == " ") {
status = 2;
return status;

// Get the next question and set the status appropriately:
// If status is not 0, return null, otherwise:
// If no lines are left before anything is read, set status
// to 1 and return null
// If the question is read but no answer left, set status to
// to 2 and return null
// If both the question and answer are read, return them in
// a new Question object.

public Question getQuestion() {
return null;

Answer Source

Well, firstly,

File Quiz = new File(f);

is not a good practice. Quiz is the name of a class. File is also the name of a class. If you want to create a File object, do this:

File quizFile = new File(f);

You shouldn't want to create a Quiz object within the Quiz constructor, as the constructor will call the Quiz constructor... and you get stuck with an infinite loop. This is probably just a naming mistake, but remember not to start variable names with capital letters and also avoid having a file object names quiz within a Quiz class, because that leads to some confusion.

So anyway your error comes from this line:


What you just did here is that you created a Scanner object, opening a stream to read from the text file, then closed it. So now its not possible for you to read from the file, because you just closed your stream. The solution to this would be to have a close() method within the Quiz class to be invoked by some other class once it is done reading from the file.


return Quiz;

Constructors don't return anything. They just construct an object of the class. Also, I'm not entirely sure but you might be confused between what a class is and what an object is. A class is just like a template for an object; the things that an object can do and what attributes it has is defined by a class. For example, in the statement

Cat bob = new Cat();

You are creating an object (bob) of the class Cat.

The statement return Quiz;, even if it were not in a constructor, means nothing because you are returning a class not an object. If you want a static method to return an object, then it should be something along the lines of

return new Cat();

not return Cat;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download