how to store the data?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

how to store the data?

 
0
  #1
Sep 12th, 2005
how to store the data read from the txt file, and store it into different array?

for example,

name,age,address

i know how to read them differently, but don know how to store it... :cry:

FileReader fr = new FileReader("abc.txt");

input = new BufferedReader(fr);
line = input.readLine();

while (line != null)
{
StringTokenizer tokens =
new StringTokenizer(line, ", \t");
String token;

while(tokens.hasMoreTokens())
{
token = tokens.nextToken();
System.out.println(token); /* trying to see what is the output, but donno how to store it to different array*/
}
line = input.readLine();

}
input.close();
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: how to store the data?

 
0
  #2
Sep 12th, 2005
Think about this situation as similar to keeping running totals. Create another variable that will store the complete file. After each read, append the read in data to the variable.

Next time, put your code in [code] tags.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: how to store the data?

 
0
  #3
Sep 12th, 2005
how to append the read in data to the variable?

that is what i donno, i've tried looking for it, but its in C...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: how to store the data?

 
0
  #4
Sep 12th, 2005
Each row contains a persons info. The best thing to do is create a class that holds each row. Here is an example:

  1. class PersonInfo
  2. {
  3. private String name;
  4. private int age;
  5. private String address;
  6.  
  7. public PersonInfo(String name, int age, String address)
  8. {
  9. this.name=name;
  10. this.age=age;
  11. this.address=address;
  12. }
  13. }

Then in your loop above you should write:
  1. //create array outside
  2. PersonInfo[] listOfInfo = new PersonInfo[100]; //change 100 to the desired size!
  3.  
  4. //maintain # of items in array
  5. int listOfInfoSize=0;
  6.  
  7. //read lines
  8. while (line != null)
  9. {
  10. StringTokenizer tokens = new StringTokenizer(line, ", \t");
  11.  
  12. //get three tokens
  13. String nameToken = tokens.nextToken();
  14. String ageToken = tokens.nextToken();
  15. String addressToken = tokens.nextToken();
  16.  
  17. //create personinfo object
  18. PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken);
  19.  
  20. //add to array
  21. listOfInfo[listOfInfoSize++]=obj;
  22. }
As you see above, the array will hold all the PersonInfo objects. Each PersonInfo object represents a row in the text file. You should add getter methods to the PersonInfo class. Then you can use this array to get any info you need.

For more help, www.NeedProgrammingHelp.com
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: how to store the data?

 
0
  #5
Sep 12th, 2005
:!: got some idea now... will try this afternoon... :cheesy:

thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: how to store the data?

 
0
  #6
Sep 13th, 2005
  1. //create array outside
  2. PersonInfo[] listOfInfo = new PersonInfo[100]; //change 100 to the desired size!
  3.  
  4. //maintain # of items in array
  5. int listOfInfoSize=0;
  6.  
  7. //read lines
  8. while (line != null)
  9. {
  10. StringTokenizer tokens = new StringTokenizer(line, ", \t");
  11.  
  12. //get three tokens
  13. String nameToken = tokens.nextToken();
  14. String ageToken = tokens.nextToken();
  15. String addressToken = tokens.nextToken();
  16.  
  17. //create personinfo object
  18. PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken);
  19.  
  20. //add to array
  21. listOfInfo[listOfInfoSize++]=obj;
  22. }

the object cannot be created. it says that "non-static variable this cannot be referenced from a static context"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: how to store the data?

 
0
  #7
Sep 13th, 2005
Which line did it have a problem with?
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: how to store the data?

 
0
  #8
Sep 13th, 2005
PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken);

Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: how to store the data?

 
0
  #9
Sep 13th, 2005
Are you creating the PersonInfo class definition in the same file as the rest of your program? If you are, you need to define the PersonInfo class as static.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: how to store the data?

 
0
  #10
Sep 13th, 2005
  1. public static void main(String[] args)
  2. {
  3. Item[] abc= new Item[10];
  4. String line;
  5. int size=0;
  6.  
  7. BufferedReader readResults = new BufferedReader(new FileReader("abc.txt"));
  8. line = readResults.readLine();
  9.  
  10. while (line!=null)
  11. {
  12.  
  13. StringTokenizer tokens = new StringTokenizer(line, ",");
  14.  
  15. while (tokens.hasMoreTokens())
  16. {
  17.  
  18. String nameToken = tokens.nextToken();
  19. int markaToken = Integer.parseInt(tokens.nextToken());
  20. int markbToken = Integer.parseInt(tokens.nextToken());
  21.  
  22. abc[size] = new Item(nameToken, markaToken, markbToken);
  23. size++;
  24.  
  25. }
  26. }
  27.  
  28. class Item
  29. {
  30. private String name;
  31. private int marka, markb;
  32.  
  33. public Item(String name, int marka, int markb)
  34. {
  35. this.name=name;
  36. this.marka=marka;
  37. this.markb=markb;
  38. }
  39. }

got another error "cannot resolve symbol"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC