• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading formatted values from file  RSS feed

 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a file I want to read as following values from files in java.

ID,Name,Gender
1,ABC,F

I can read those values from files. However, I don't know how to get to know value 'F' is Gender. value 'ABC' is name.

How do I differentiate those values with the user provided format?

Thanks, goutgout
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claire,

How about creating a class which contains these three as attributes?
For e.g. you could create a simple Person class with three fields- id, name and gender. Then each row in the text file(except the first one) can be mapped to a Person object. You could then add methods in this class to retrieve a person's details based on your requirements.
You could even create a List of these Person objects to map all the rows in the file if you need it.

Edit- I think I misunderstood your question(I now think that you meant that the sequence of the columns is random), Dave's comment below has the more appropriate answer.
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can split the first line (which has the headers) and store those somewhere so you know column 0 is ID, column 1 is Name and column 2 is gender.
What you do with that knowledge depends on your model I suppose.
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, i will test using table in which file format is saved. btw why i can' use emotion icon here hehe
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it actually works if I create table but another step is i need to create dynamic table base on format: for eg,

If format is "ID, Name, Address", I need to create table with column names (ID, Name, Address". If format is "ID, Address, Name, DOB", I need to create table with columns (ID, Address, Name, DOB).

Any pointer would be appreciated about how to create dynamic sql table in java?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you want to do; your question now looks completely different from its original form. What do you mean by a dynamic table?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!