954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to store the data?

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();
}

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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.

chrisbliss18
Posting Shark
917 posts since Aug 2005
Reputation Points: 38
Solved Threads: 25
 

how to append the read in data to the variable? :sad:

that is what i donno, i've tried looking for it, but its in C...

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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

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:

//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;
}

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
Junior Poster in Training
55 posts since May 2005
Reputation Points: 10
Solved Threads: 1
 

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

thanks! :D

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 
//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"

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Which line did it have a problem with?

chrisbliss18
Posting Shark
917 posts since Aug 2005
Reputation Points: 38
Solved Threads: 25
 

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

;)

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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.

chrisbliss18
Posting Shark
917 posts since Aug 2005
Reputation Points: 38
Solved Threads: 25
 
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"

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 
import java.io.*;
import java.util.*;

public class test 
{
	public static void main(String[] args) 
	{
		Item[] abc= new Item[10];
		String line, subjectName;
		int size=0;
	
		try
		{
		BufferedReader readResults = new BufferedReader(new FileReader("abc.txt"));
	
		subjectName = readResults.readLine();
		line = readResults.readLine();
		
		while (line!=null)
		{
	
		
			StringTokenizer tokens = new StringTokenizer(line, ",",false);
			String nameToken = tokens.nextToken();
			if((tokens.hasMoreTokens()) );
			int markaToken = Integer.parseInt(tokens.nextToken());
			int markbToken = Integer.parseInt(tokens.nextToken());

			abc[size] = new Item(nameToken, markaToken, markbToken);
			size++;
			line = readResults.readLine();
		
		}
		System.out.println(subjectName);  /* this 4 lines is to check whether the infomation have been stored*/
		System.out.println(abc[0].getname());
			System.out.println(abc[1].getmarka());
				System.out.println(abc[2].getmarkb());
				
		}
		
	
		catch(FileNotFoundException e)
		{}
	
		catch(IOException e)
		{}
	}
	
}

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;
	}
	public double getmarka()
	{
		return marka;
	}
	
	public double getmarkb()
	{
		return markb;
	}
	
	public String getname()
	{
		return name;
	}
	
}


i finally got it correct... thanks to those look at the thread and help me! :D

boyzz
Newbie Poster
12 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You