• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating Objects from Text File  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to create an entire University object filled with arrays os Students, Courses, and Instructors. All of this will be stored in a text file. How do I read the information in and create the objects. I know that I will be using constructors but how will I tell the program which strings/objects to read? For example, my file looks like this:
How would I tell it to read "University of Florida" as the University Name, and get it to skip over reading "University Name: "

Thanks for any help!
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing text isn't a trivial task. However, if you have strict control over the text format, that helps a lot.

Do you know how to read text from a file? Assuming, you can get that first line into a Java String, you can use one of the String.substring() methods to get just the part you want.

Good luck!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:
Do you know how to read text from a file? Assuming, you can get that first line into a Java String, you can use one of the String.substring() methods to get just the part you want.

Good luck!


Given the format he's shown, substring() by itself one be enough. He'll also need indexOf. (Alternatively, String.split, but let's not go there yet...)
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would I do something like this to get the University Name??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kd Martin wrote:So would I do something like this to get the University Name??


After reading the substring docs, what do you think that will do, exactly?
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would store the variable "name" so then I would have to use the University class' constructor. Correct?
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kd Martin wrote:That would store the variable "name" so then I would have to use the University class' constructor. Correct?

No. You need to check what the parameters to the substring method represent.
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the document's structure is consistently [field_name]: [value], then your first priority using substring is to find out where in the String that ':' is located.
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I should use ":" as a delimeter and then do scanner.next() to get the values and then use a compiler to make objects with these values?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!