| | |
how to store the data?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2005
Posts: 12
Reputation:
Solved Threads: 0
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();
}
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();
}
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.
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
Each row contains a persons info. The best thing to do is create a class that holds each row. Here is an example:
Then in your loop above you should write:
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
Java Syntax (Toggle Plain Text)
class PersonInfo { private String name; private int age; private String address; public PersonInfo(String name, int age, String address) { this.name=name; this.age=age; this.address=address; } }
Then in your loop above you should write:
Java Syntax (Toggle Plain Text)
//create array outside PersonInfo[] listOfInfo = new PersonInfo[100]; //change 100 to the desired size! //maintain # of items in array int listOfInfoSize=0; //read lines while (line != null) { StringTokenizer tokens = new StringTokenizer(line, ", \t"); //get three tokens String nameToken = tokens.nextToken(); String ageToken = tokens.nextToken(); String addressToken = tokens.nextToken(); //create personinfo object PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken); //add to array listOfInfo[listOfInfoSize++]=obj; }
For more help, www.NeedProgrammingHelp.com
•
•
Join Date: Sep 2005
Posts: 12
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
//create array outside PersonInfo[] listOfInfo = new PersonInfo[100]; //change 100 to the desired size! //maintain # of items in array int listOfInfoSize=0; //read lines while (line != null) { StringTokenizer tokens = new StringTokenizer(line, ", \t"); //get three tokens String nameToken = tokens.nextToken(); String ageToken = tokens.nextToken(); String addressToken = tokens.nextToken(); //create personinfo object PersonInfo obj = new PersonInfo(nameToken, ageToken, addressToken); //add to array listOfInfo[listOfInfoSize++]=obj; }
the object cannot be created. it says that "non-static variable this cannot be referenced from a static context"
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Sep 2005
Posts: 12
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
public static void main(String[] args) { Item[] abc= new Item[10]; String line; int size=0; BufferedReader readResults = new BufferedReader(new FileReader("abc.txt")); line = readResults.readLine(); while (line!=null) { StringTokenizer tokens = new StringTokenizer(line, ","); while (tokens.hasMoreTokens()) { String nameToken = tokens.nextToken(); int markaToken = Integer.parseInt(tokens.nextToken()); int markbToken = Integer.parseInt(tokens.nextToken()); abc[size] = new Item(nameToken, markaToken, markbToken); size++; } } class Item { private String name; private int marka, markb; public Item(String name, int marka, int markb) { this.name=name; this.marka=marka; this.markb=markb; } }
got another error "cannot resolve symbol"
![]() |
Similar Threads
- how to store and retrieve data from cookies (ASP.NET)
- C++ and data access (C++)
- Store files data into specified memory (RAM) address - help? (Assembly)
- Automatic Data ENtry (Visual Basic 4 / 5 / 6)
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- passing parameter to Store Procedure in SqlDataAdapter (VB.NET)
- Using data i read from *.* files (C++)
- Binary data in app.config (C#)
- Internal Data Tables (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: finding repeated subsequence in string
- Next Thread: Please help me in string operation problem
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows





