Printing a method return when using an array as a parameter
I'm wondering why my println(arrayAvg()) statements are not producing any results. Could someone point me in the right direction? Below is my program:
There's also a link here: Ideone.com | Online Java Compiler & Debugging Tool
This is a sample of the file I'm reading:
STATION DATE PRCP SNOW SNWD TMAX TMIN
----------------- -------- -------- -------- -------- -------- --------
GHCND:USW00014764 20110101 0 0 127 122 -17
GHCND:USW00014764 20110102 5 0 102 67 28
GHCND:USW00014764 20110103 13 0 76 44 -56
Code:
import java.io.*;
import java.util.*;
import java.awt.*;
public class Array {
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner(new File("PortlandWeather2011.txt"));
String x = input.nextLine();
System.out.println(x);
String y = input.nextLine();
System.out.println(y);
int count = 0;
while (input.hasNextLine()) {
process(input);
count++;
}
double[] precip = new double[count];
double[] snow = new double[count];
double[] snowDepth = new double[count];
double[] tempMin = new double[count];
double[] tempMax = new double[count];
input = new Scanner(new File("PortlandWeather2011.txt"));
x = input.nextLine();
System.out.println(x);
y = input.nextLine();
System.out.println(y);
count = 0;
while (input.hasNextLine()) {
input.next();
input.next();
precip[count] = input.nextDouble();
snow[count] = input.nextDouble();
snowDepth[count] = input.nextDouble();
tempMax[count] = input.nextDouble();
tempMin[count] = input.nextDouble();
count++;
}
System.out.println(arrayAvg(precip));
System.out.println(arrayAvg(snow));
System.out.println(arrayAvg(snowDepth));
System.out.println(arrayAvg(tempMin));
System.out.println(arrayAvg(tempMax));
}
public static double arrayAvg(double a[]) {
int count = 0;
double sum = 0;
for (int i = 0; i < a.length; i++) {
count++;
if (a[i] != 393.7) {
sum += a[i];
}
}
return sum/count;
}
public static void process(Scanner input) {
while (input.hasNext()) {
String station = input.next();
while (input.hasNextInt()) {
String date = input.next();
while (input.hasNextInt()) {
int precipitation = input.nextInt();
while (input.hasNextInt()) {
int snow = input.nextInt();
while (input.hasNextInt()) {
int snowDepth = input.nextInt();
while (input.hasNextInt()) {
int tempMin = input.nextInt();
while (input.hasNextInt()) {
int tempMax = input.nextInt();
}
}
}
}
}
}
}
}
public static double tempConvert(int n) {
double celsius = (n / 10.0) * 9 / 5 + 32;
return celsius;
}
public static double snowConvert(int n) {
double snowInches = n * 0.03937;
return snowInches;
}
public static double precipitationConvert(int n) {
double rainInches = ((n * 0.03937) / 10);
return rainInches;
}
}
Re: Printing a method return when using an array as a parameter
You'll need to debug it.
Stick some println()'s in there, to print out what's been read from the file, what the content of the arrays is, what values 'sum' and 'count' are when you do your calculation.
Essentially you need to narrow down where the problem lies.