wood burning stoves*
The moose likes Java in General and the fly likes read text and stroe date to the array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "read text and stroe date to the array" Watch "read text and stroe date to the array" New topic
Author

read text and stroe date to the array

Rayt Leeop
Greenhorn

Joined: Jun 09, 2011
Posts: 23
Hi, I have data.text file. The line is
A, 2, 3,4 ,4, B,2,3,5,6,C,34,67,8,6.....

ABC... is continued and there are 4 number following one letter. I want read the data and then store them in an array.
I did create a constructor like this: Any one can help me how to store the number in a array? Many thanks

Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Once you get the line:

use the split() method of String class to split the line from the places where you have "A," etc.
for example:
if your line is: "A,2,3,4,4,B,2,3,5,6,C,34,67,8,6"
then after splitting you will get one String array containing: {"2,3,4,4","2,3,5,6","34,67,8,6"}
you can iterate through this array and split each element string with "," and get your numbers.

Hint: construct a regex for first splitting.


Piyush
Rayt Leeop
Greenhorn

Joined: Jun 09, 2011
Posts: 23
Thanks, can we use other mthods like two dimension? Can you shoe me how to use split? i need also link the result to the GUI
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

This example shows how you can use split method in this case.


can we use other mthods like two dimension?

are you talking about 2 dimensional string arrays? have you tried something?
Rayt Leeop
Greenhorn

Joined: Jun 09, 2011
Posts: 23
Many thanks. Actually, I am doing a project similar to http://www.cs.utexas.edu/~scottm/cs307/Assignments/BabyNames.html

i need to implement several methods like :
1: The constructor should take a String parameter. You will pass each line of the
text file to the constructor which will setup the data in the NameRecord object.

b. String getName() which returns the name

c. int getRank(int year) which returns the ranking for the passed year (1900, 1910,
1920, etc.)

d. int getBestYear() – returns the year (1900, 1910, 1920, etc.) the name was the
most popular. If there is a tie between two decades, it will return the earliest
year.

how can I link the rank/best year to the array so that the GUI interface can be sued? Give me give me some examples on the above c and d?

Many thanks.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

you can store these year-rank pairs in a sorted map, where year will be key and rank in that year will be the value.
Thus getting the rank for a particular year will be easy.
and for getting the best year, first get set of all the values in the map, then find the greatest value, then iterate over the map to get first key (e.i. earliest year) for that value.

I think that project expects you to have a practice of Java's collections framework.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: read text and stroe date to the array
 
Similar Threads
array question?
Reading from XML file
Adding 2-d ArrayList to JTable
Reading one line from a text
Error printing out from a read in file.