dcsimg
Enter a brief summary here!_NEWLINE_!reading and storing data from a textfile into an array
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   fabrice_khlok
Posted On:   Wednesday, July 31, 2002 07:44 AM

hey guys i have a question: I am a newcomer to java, maybe someone can help. I am working on an excercise and am stuck. the following table is given: months jan feb mar ......dec hoursofsun 211 220 322 ....... I need to store both the months and hoursofsun into two 1-dimensional arrays. how can i read the names of months from the text and store them into seperate cells? what i did was: String[] months : {"jan","feb", "mar".....] but i would like to know how to do it the other way. second part: for the second array: I did the follo   More>>

hey guys i have a question:

I am a newcomer to java, maybe someone can help. I am working on an excercise and am stuck.

the following table is given:

months jan feb mar ......dec
hoursofsun 211 220 322 .......

I need to store both the months and hoursofsun into two 1-dimensional arrays.

how can i read the names of months from the text and store them into seperate cells?

what i did was:
String[] months : {"jan","feb", "mar".....]
but i would like to know how to do it the other way.

second part:

for the second array:
I did the following and got some jind of runtime error:
I'm not sure what's wrong, but could someone tell me how to store numbers from a file (on one line) into individual cells?
thx

{
final int SIZE = 12;

StringTokenizer data;

FileReader file = new FileReader("a:\hours.txt");
BufferedReader inputFile = new BufferedReader(file);

int[]hours = new int[SIZE];

for (int index=0; index != SIZE; index++)
{
data = new StringTokenizer(inputFile.readLine());
hours[index] = new Integer(data.nextToken()).intValue();
}
for (int index=0; index != SIZE; index++)
{
screen.println(hours[index]);
}
}

   <<Less

Re: Enter a brief summary here!_NEWLINE_!reading and storing data from a textfile into an array

Posted By:   David_Lin  
Posted On:   Wednesday, July 31, 2002 05:44 PM

What exactly was the error? It would help if you can paste the error message in...

Some possible answers for your 2nd question:

Make sure hours.txt does not have extra blank lines before your data, or data.nextToken() will throw a NoSucElement exception.

Make sure the token you read in (data.nextToken()) is actually a number, or Integer constructor will throw a NumberFormat exception.

David Lin

About | Sitemap | Contact