• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

read txt file and store the information in a class

 
Greenhorn
Posts: 7
Python VI Editor C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7
Python VI Editor C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7
Python VI Editor C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic