hello
hello

Reputation: 77

Create object for each line of text file

I'm trying to do something relatively simple; to read in a text file line by line and then create a 'Course' object for each line. I then want to add each object to a new 'CourseList'object that will be returned in a instance method.

This is what I have so far: (I just included my try part in method)

try {

  FileReader reader = new FileReader(inputName);
  BufferedReader inFile = new BufferedReader(reader);

  String nextLine = inFile.readLine();
  Course newObject = new Course();
  CourseListQ2 newList = new CourseListQ2();//entire object to be returned

  while(nextLine!=null){
    newList.addCourse(newObject);
  } 
  return newList;

  inFile.close();
}

I'm getting an error for the Course newObject = new Course(); line that says:

Error: constructor Course in class Course cannot be applied to given types; required: java.lang.String,java.lang.String,int

So I don't think I'm understanding how to create a Course object for each line. Or even what that truly means. Any suggestions?

Upvotes: 1

Views: 4259

Answers (4)

user2575725
user2575725

Reputation:

There are few corrections in your code:

Course newObject = new Course();//constructor requires 3 params of String, String and int, what you get as compile time error.
CourseListQ2 newList = new CourseListQ2();
while(nextLine!=null){//you are not reading the next line, so it will be a infinite loop after reading the first line of file
    newList.addCourse(newObject);//adding same course object instead you need different courses from each line
} 
return newList;//must be after the below line
inFile.close();//in future compiler is gona give error for writing below return keyword

Edited You may try this code:

CourseListQ2 newList = new CourseListQ2();
BufferedReader inFile = null;
try {
  inFile = new BufferedReader(new FileReader(inputName));
  String nextLine;
  String param1;
  String param1;
  int param3;
  while(null != (nextLine = inFile.readLine())){//read line by line
    param1 = your code here to play with nextLine
    param2 = your code here to play with nextLine
    param3 = your code here to play with nextLine
    newList.addCourse(new Course(param1, param2, param3));
  }
} catch(IOException ex) {
   ex.printStackTrace(System.err);//print exception on console
} finally {//its optional but recommended
  if(null != inFile) {
     try {
        inFile.close();//may cause trouble
     } catch(IOException ex) {
        ex.printStackTrace(System.err);//print exception on console
     }
  }
  return newList;
}

Upvotes: 2

Milind J
Milind J

Reputation: 517

Check your default constructor, are you chaining to use another constructor using something like, this(someparam, someparam, someparam). The parameters passed from your default constructor do not match those for one of your other constructors

Upvotes: 1

user2704764
user2704764

Reputation:

If you need to add a new course object for each line then you've gotta move somethings around.

try {

  FileReader reader = new FileReader(inputName);
  BufferedReader inFile = new BufferedReader(reader);

  String nextLine = inFile.readLine();
  Course newObject;
  CourseListQ2 newList = new CourseListQ2();//entire object to be returned

  while(nextLine!=null){
    newObject = new Course(); //There are paramaters that the constructor                
                              //requires that you need to include
    newList.addCourse(newObject);
  } 
  return newList;

  inFile.close();
}

In addition to moving the object creation into the while loop (therefore making a new object for each line) you also need to correct the line

newObject = new Course();

According to your error, you are trying to create an object using a constructor that doesn't exit. If you look at your Course class you'll find that there is no constructor without any parameters. You have two choices: add a constructor without parameters, or change the line above to call on a different constructor.

newObject = new Course(String, int, int);

Upvotes: 1

Astra Bear
Astra Bear

Reputation: 2738

The constructor for your Course Object needs three arguments: String, String and int.

You can either supply these arguments or add the contructor without arguments to your Course class.

Upvotes: 1

Related Questions