Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Text file to ArrayList  RSS feed

 
Domain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file and now am suppose to read the text file and assign it to ArrayList<CourseSection> schedule.

This is what I have so far:

ArrayList<CourseSection> schedule = new ArrayList<CourseSection>();
while(fileReader.hasNextLine())
for (int index = 0; index < schedule.size(); index++)
{
schedule.add(fileReader.nextLine())
}
When I complie I get: cannot find symbol method add(java.util.String)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess fileReader is a Scanner. The nextLine() method returns a String. Since you've declared your List as an ArrayList<CourseSection>, the add() method expects the argument to be a CourseSection object, not a String. Can you convert the String into a CourseSection object before adding it to the ArrayList? Or, you could use an ArrayList<String> instead.
 
Domain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your post. I will try your suggest. The next problem that I am having is the reader puts everything on one line and the file is a list with ten things.
 
Domain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is a CourseSection object? Not sure I understand.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a data type that you have written as a Java Object, or it is in the source code of a project that you are working on.
 
Domain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had no success in: converting the String into a CourseSection object. Can somebody help?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Domain", please check your private messages. Thanks.

No one else here knows anything about this "CourseSection" thing. That's something that came from your code. Or code that someone else wrote, but we can't see it. Where did you hear of "CourseSection"? Is it a class that you can look at? Does the class have any constructors, or static methods that return a CourseSection object?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!