• Post Reply Bookmark Topic Watch Topic
  • New Topic

read txt file and store the information in a class  RSS feed

 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have the txt file as following (just made up to demonstrate)

`name: Alex ID: 123456 latest_login: 2016.11.23 10:11:22 current_status: offline
name: Jack ID: 234567 latest_login: 2015.2.2 00:11:33 current_status: offline`

and I want to store all the information to a class, let's say `class User` with `String name, int ID, Date latest_login, boolean current_status`, so how do I store the information from txt file to a Java class? How to choose the part only after ": "?

Any help is appreciated, thanks in advance!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dex,
The first part is to read in the file. What version of Java are you using?
 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Dex,
The first part is to read in the file. What version of Java are you using?


JDE 8, so for reading file I was going to use:

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that looks like a reasonable enough way to read lines of text from a file. So far all you do is to echo the lines to the console, but one step at a time, right? Now you want to replace that code with whatever should really happen to the lines of text.

That's where I get a bit mixed up. You say you want to store the data to a class, which is rather a confusing way to describe what you really want to do. My guess is that for each line of code you want to create a User object, then you want to extract the four data items from the line of text, then put the four data items into four corresponding attributes of the User object. Or something similar to that. Am I correct in assuming that the User class already exists?

I'd suggest that your next step is to extract the four data items from the line of text and convert them from text format into the correct Java data types. The step after that would be to create a User object and populate it with the extracted data, but don't get trapped into trying to do both things at once. That just gets too confusing. Extract the data items first and get that working before starting to mess with User objects.
 
Dex Hunter
Greenhorn
Posts: 6
C++ Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yup, that looks like a reasonable enough way to read lines of text from a file. So far all you do is to echo the lines to the console, but one step at a time, right? Now you want to replace that code with whatever should really happen to the lines of text.

That's where I get a bit mixed up. You say you want to store the data to a class, which is rather a confusing way to describe what you really want to do. My guess is that for each line of code you want to create a User object, then you want to extract the four data items from the line of text, then put the four data items into four corresponding attributes of the User object. Or something similar to that. Am I correct in assuming that the User class already exists?

I'd suggest that your next step is to extract the four data items from the line of text and convert them from text format into the correct Java data types. The step after that would be to create a User object and populate it with the extracted data, but don't get trapped into trying to do both things at once. That just gets too confusing. Extract the data items first and get that working before starting to mess with User objects.


Thanks, now I know what I should look for!
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I suggest that the code to do that part (extraction and creation of a user object) be done in its own method, rather than trying to squeeze it all into that existing one:



Don't think you have to create a valid User straight away.
Initially that can just show:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!