Results 1 to 2 of 2
- 11-28-2012, 07:40 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
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
Java 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; } }Last edited by JosAH; 11-28-2012 at 07:51 PM. Reason: fixed [code] ... [/code] tags
- 11-29-2012, 09:42 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
Similar Threads
-
problem in a method which return array format
By elenora in forum Advanced JavaReplies: 13Last Post: 09-21-2011, 09:43 AM -
Can i Pass a variable as a parameter from one method to other method
By Anagha in forum New To JavaReplies: 18Last Post: 04-18-2011, 05:39 AM -
Creating array in method parameter
By Dipke in forum New To JavaReplies: 2Last Post: 02-25-2011, 09:18 AM -
Accessing return value without printing
By minihazard10 in forum New To JavaReplies: 2Last Post: 10-18-2008, 07:46 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks