# Need help with Code

• 11-21-2010, 02:50 AM
billyR
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.
• 11-21-2010, 07:19 AM
Eranga
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.
• 11-21-2010, 07:19 AM
Eranga
Actually the first step is to read the file. Did you do that?
• 11-21-2010, 09:21 AM
billyR
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.
• 11-21-2010, 11:16 AM
Eranga
You can initialize arrays as follows, but you must know the size of it in advance.

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?
• 11-21-2010, 11:17 AM
Eranga
Did you search our forum or the web? If so you can find lots of related stuff.

Read the following on Suns' tutorial.

Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
• 11-21-2010, 08:28 PM
billyR
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.
• 11-21-2010, 08:47 PM
pbrockway2
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.

Code:

`Toronto, Canada 6 7 7 6 6 6 6 7 8 9 8 9`

it ought to be possible to output

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.
• 11-21-2010, 09:07 PM
billyR
And just run a for-loop through each line to print out the city name and the average of the temperature?
• 11-21-2010, 09:26 PM
pbrockway2
Yes, a for loop could be used to extract each "token" from a Scanner which you then process to obtain an average.
• 11-22-2010, 04:02 AM
Eranga
Quote:

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.
• 11-22-2010, 04:07 AM
Fubarable
Quote:

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-22-2010, 04:25 AM
Eranga
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.