Results 1 to 13 of 13
  1. #1
    billyR is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    billyR is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    billyR is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  9. #9
    billyR is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    And just run a for-loop through each line to print out the city name and the average of the temperature?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Yes, a for loop could be used to extract each "token" from a Scanner which you then process to obtain an average.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by billyR View Post
    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Eranga View Post
    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.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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