• 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

reading formatted values from file

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic