d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help
Add Reply New Topic New Poll
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Sep 22 2013 12:44pm
I'm writing a program to read a text file that displays the highest and lowest temperature for each month and you could say that I am very lost right now
this is what I have for now I'm going to keep googleing/reading and try to get further but if you see a blatant mistake which I'm sure there are tons any tips or help would be appreciated


Code
import java.io.*;
import java.util.*;


public class TempExercise {

public static void main(String[] args) throws FileNotFoundException{

getData();
averageHigh();
averageLow();
indexHighTemp();
indexLowTemp();

}

public double[] getData() throws FileNotFoundException{
String FILENAME = ("TomsRiverMaxMintemps.txt");

double[] low = new double[12];
double[] high = new double[12];

Scanner inFile = new Scanner(new FileReader(FILENAME));

for(int index=0; index < high.length; index++)
high[index] = inFile.nextDouble();

inFile.nextLine();

for(int index=0; index < low.length; index++)
low[index] = inFile.nextDouble();

inFile.close();
return high;
return low;
}
public double averageHigh(double high[]){
double sum = 0;


for (int i = 0; i < high.length; i++)
sum = sum + high[i];


double averageHigh = sum / high.length;

return averageHigh;
}
public double averageLow(double low[]){
double sum = 0;
for (int i = 0; i < low.length; i++)
sum = sum + low[i];


double averageLow = sum / low.length;

return averageLow;
}

public double indexHighTemp(double high[] ){
int maxIndex = 0;

for(int i = 0; i < high.length; i++)
if(high[maxIndex] < high[i])
maxIndex = i;
double maxTemp = high[maxIndex];

return maxTemp;
}
public double indexLowTemp(double low[] ){
int minIndex = 0;

for(int i = 0; i < low.length; i++)
if(low[minIndex] > low[i])
minIndex = i;
double minTemp = low[minIndex];

return minTemp;
}
}
Member
Posts: 16,411
Joined: Apr 9 2007
Gold: 12,059.17
Sep 24 2013 03:29pm
I dont know whats your level of object programming knowledge.

I will assume you know nothing about object programming and propose simplest but not the most elegant solutions.

first of all: methods that you call in your code need to be static:
getData
averageHigh
averageLow
indexHighTemp
indexLowTemp
in example: rewrite "public double[] getData()" into "public static double[] getData()"
rule to remember: use static word in all functions unless you learn object programming :)

second of all: you can not return two values from java function:
return high;
return low;

Usually one would return a single more complex object, but for sake of simplicity you can just write two methods:
getDataLow
getDataHigh
and read the file twice :)

3rd of all:
after you return a value from getData you need to store it somewhere and pass into second function. In your current code value returned from getData is lost...
so for example lines:

getDataLow();
averageLow();

should be rewritten into:

double[] lowData = getDataLow();
averageLow(lowData);

4th of all: your code doesnt print anything, it only does silent processing. For printing information on screen read about System.out.println

Thats it for start, let me know if thats understandable.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll