# Thread: Need help with Code

1. Member
Join Date
Nov 2010
Posts
4
Rep Power
0

## Need help with Code

Hey guys,

My teacher gave us an assignment too read in information from a .dat file, the first few lines of the file looks like this.

Toronto, Canada 6 7 7 6 6 6 6 7 8 9 8 9
California, United States 13 12 11 15 14 13 13 13 12 12 11
Sydney, Australia n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a

We need to output the average of all the temperatures for the City's, obviously some of the city's have n/a which means they don't have the date. The output should look like this.

California, United States 12.35742334
Sydney, Australia n/a

I really don't know how to do this since I have been sick from class and really don't know to many people in the class. So I came to this forums for some help. If you guys could help me get started, that would be great.

I think I would need to store everything in a double array and use a for-loop to calculate the average and output it to the user. But not 100% sure.

2. Welcome to forum. :)

First of all show what you've done so far. Then only we can help you to do this. No one here wants to do assignments for you. So show up your attempt here.

3. Member
Join Date
Nov 2010
Posts
4
Rep Power
0
All my work is at school and all I'll have is Monday to work on it. I have read everything in but I don't know how to store it into a double array. If I can get that step done I'm positive I can get everything else done.

4. You can initialize arrays as follows, but you must know the size of it in advance.

Java Code:
`double[] dArray = new double[10];`
where the array can hold maximum of 10 elements.

Read from the file and insert into the arrays. But as I said you must know the size of the array when initializing. Do you know that? Think about it and give a try.

By the way, if you really want to learn Java I wonder that why those things are not in your home PC. This is not a class assignment right, is it a take home assignment?

5. Member
Join Date
Nov 2010
Posts
4
Rep Power
0
It's an inclass assignment that we will be doing tomorrow. I know how to initialize an array I just need to know how to bring in the file I'm reading in into a double array.

Way too confusing, I have been trying to mess around but not getting any luck.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Unless I'm missing something there's nothing in the problem that suggests a double array. The file can be read a line at a time and the output generated.

Java Code:
`Toronto, Canada 6 7 7 6 6 6 6 7 8 9 8 9`

it ought to be possible to output

Java Code:
`Toronto, Canada 6.788888888889`

and then move on to the next line.

A single array might be involved (one element for each temperature reading), but not a double one.

7. Member
Join Date
Nov 2010
Posts
4
Rep Power
0
And just run a for-loop through each line to print out the city name and the average of the temperature?

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Yes, a for loop could be used to extract each "token" from a Scanner which you then process to obtain an average.

9. Originally Posted by billyR
And just run a for-loop through each line to print out the city name and the average of the temperature?
Yes, loop of each line of the file.

Then tokanize each line by the space. Since each line has well define format with the space it's easy. Then keep first two elements separately, city as Toronto, Canada. Then rest of the stuff all you need to add into the double array, that is from the third element of the tokens you've.

10. Originally Posted by Eranga
Then tokanize each line by the space. Since each line has well define format with the space it's easy. Then keep first two elements separately, city as Toronto, Canada. Then rest of the stuff all you need to add into the double array, that is from the third element of the tokens you've.[/COLOR]
It'll take a little more logical tweaking as some of the Cities and countries may contain two words separated by a space such as United States and New York. One way to do this is to split the String/line as already mentioned with space and then check each token to see if it's a number and if it is, start adding your numbers and counting the number of numeric items on the line so you can calculate an average. If you hit the end of the line and no number has been found, then output an N/A for that line.

11. Of course. I just give him a methodology. Should design properly. That could solve how the number are identified. If OP just hang with the index then it's real mess. But validating the format then it's not a problem at all, that's how try-catch use in tricky way.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•