Results 1 to 13 of 13
Thread: Need help with Code
- 11-21-2010, 01:50 AM #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.
Toronto, Canada 6.788888888889
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, 06:19 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:19 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually the first step is to read the file. Did you do that?
- 11-21-2010, 08:21 AM #4
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.
- 11-21-2010, 10:16 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can initialize arrays as follows, but you must know the size of it in advance.
where the array can hold maximum of 10 elements.Java Code:double[] dArray = new double[10];
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, 10:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 07:28 PM #7
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.
- 11-21-2010, 07:47 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
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.
For instance reading
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.
- 11-21-2010, 08:07 PM #9
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?
- 11-21-2010, 08:26 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
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, 03:02 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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, 03:25 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Similar Threads
-
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks