944,081 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9248
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 12th, 2005
0

how to store the data?

Expand Post »
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();
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005
Sep 12th, 2005
0

Re: how to store the data?

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 12th, 2005
0

Re: how to store the data?

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005
Sep 12th, 2005
0

Re: how to store the data?

Each row contains a persons info. The best thing to do is create a class that holds each row. Here is an example:

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005
Sep 12th, 2005
0

Re: how to store the data?

:!: got some idea now... will try this afternoon... :cheesy:

thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005
Sep 13th, 2005
0

Re: how to store the data?

Java Syntax (Toggle Plain Text)
  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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005
Sep 13th, 2005
0

Re: how to store the data?

Which line did it have a problem with?
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 13th, 2005
0

Re: how to store the data?

PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken);

Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005
Sep 13th, 2005
0

Re: how to store the data?

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 13th, 2005
0

Re: how to store the data?

Java Syntax (Toggle Plain Text)
  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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyzz is offline Offline
12 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: finding repeated subsequence in string
Next Thread in Java Forum Timeline: Please help me in string operation problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC