Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Store the maximum hourly temperatures for each day of a week from the text file weektemp.txt

  Asked By: Matilda    Date: May 16    Category: Java    Views: 867
  

I've just started learning Java/BlueJ at uni, and I need a bit of help
with one of my assignments.

The assignment is to store the maximum hourly temperatures for each
day of a week from the text file weektemp.txt.
Manipulate the stored data to:
- find the hottest and coldest hours
- average daily temperature
- temperature for a specific hour
and
- the daily temperature range.

Part A:
Do the above using an array of arrays.

Part B:
Do the above using an array of Day objects.

I didn't have a problem with part A, or the manipulation of the data;
I dimensioned my array as:
double[][]weekTemps = new double[7][24];
read the data file with BufferedReader, tokenized the string and
placed each token into its appropriate array element.
And this all worked fine.

However with part B I can create the array Week of Day objects (which
also contains a array of doubles called Hours).
My problem is that I can't seem to work out how to read the data from
the text file into this new array of Hours inside the array of Day
objects.
My array of Day object looks like this:
Day[] days = new Day[12];
days[0] = new Day(24);
days[1] = new Day(24);
days[2] = new Day(24);
days[3] = new Day(24);
days[4] = new Day(24);
days[5] = new Day(24);
days[6] = new Day(24);

and in my Day object:
double[] hours = new double[24];

I tried reading a line at a time and sending each new line to a new
Day object to be tokenized there, however this didn't work properly
and on asking my lecturer he aid he didn't want it done that way anyway.

Could someone pleeeeeease help me as this is driving me nuts.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Della Simpson     Answered On: May 16

Unfortunately I can't offer you any help  on this one at the moment as
I too am having the exact same problem.
So if you do manage to work  it out, please post it and I will do likewise.

However I did notice a possible problem  with your array.
I think it should be : Day[] days  = new Day[7]; not new Day[12]

 
Answer #2    Answered By: Devrim Yilmaz     Answered On: May 16

I'm not sure if this will work  or not, as I haven't tried it out yet.
This just came to me a this ungodly hour of the morning.
How about:
Day[x].Hour[y] to refer to the yth position in the hour array  in
the xth Day object.
Surely It can't be this simple.
Like I said I haven't tried it yet, I'm going back to sleep.

 
Answer #3    Answered By: Hababah Younis     Answered On: May 16

I was watching this thread slightly and it breaks my heart when I see this.
Have you heard of OOP ever?

What about creating objects  (classes) that reflects your entities?

Working with arrays  in this way is definitely not OOP. Are you guys
switching over from C? At least it looks like someone is porting
C to Java.

public class Month {

private int numberOfDays;
private String name;

public Month(String name,int numberOfDays) {
this.name = name;
this.numberOfDays = numberOfDays;
}

getter and setter ommited for bravity
}

and so on and so on and so on

 
Answer #4    Answered By: Edfu Massri     Answered On: May 16

I know what you mean. In java you should never use/have to
use arrays, use ArrayList or Vector instead. Unfortunately
at uni I have the same predicament - if the assignment  says
use arrays  the I have to use arrays even though my lecturer
tells us how much he dislikes them.

 




Tagged: