Results 1 to 2 of 2
  1. #1
    pricklypete is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

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

    ** This space for rent **

Similar Threads

  1. problem in a method which return array format
    By elenora in forum Advanced Java
    Replies: 13
    Last Post: 09-21-2011, 09:43 AM
  2. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  3. Creating array in method parameter
    By Dipke in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 09:18 AM
  4. Accessing return value without printing
    By minihazard10 in forum New To Java
    Replies: 2
    Last Post: 10-18-2008, 07:46 AM
  5. Replies: 2
    Last Post: 04-16-2008, 12:35 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
  •