Results 1 to 3 of 3
- 02-26-2013, 08:00 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Code Error+ problems with inputting array lists into methods?
Heres the code I'm having trouble with
The problem I'm experiencing is "error: non-static method totalRain(double[]) cannot be referenced from a static context". Which Im guessing
is caused because of improper method calling or inputting rainfall. Is there any way to have my rainfall array be used by the methods?
Java Code:/** * @(#)Ch7_Q1.java * * Ch7_Q1 application * * @author Peter Wu CSC 210 * @version 1.00 2013/2/14 *Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the follow ing; • the total rainfall for the year • the average monthly rainfall • the month with the DOM rain • the month with the least rain */ import java.util.Scanner; public class Ch7_Q1 { public static void main(String[] args) { Scanner keyboard=new Scanner(System.in); final int month=12; double[] rainfall= new double[month]; for (int x=0;x<12;x++) { System.out.println("What was the rainfall for month "+(x+1)+":"); rainfall[x]=keyboard.nextDouble(); if(rainfall[x]<0) {System.out.println("Please input a valid rainfall number:"); rainfall[x]=keyboard.nextDouble(); } } //These are the parts Im having trouble with System.out.println("The total rainfall is "+totalRain(rainfall)); System.out.println("The avg rainfall is "+ avgRain(rainfall)); System.out.println("The month with the lowest rainfall is month "+minRain(rainfall)); System.out.println("The month with the most rainfall is month "+maxRain(rainfall)); } //Methods public double totalRain(double[] i) {double sum=0; for (int x=0;x<12;x++){ sum+=i[x]; } return sum; } public double avgRain(double total) {double avg=total/12; return avg; } public double maxRain(double[] i) {double max=i[1]; int month=0; for (int x=0;x<12;x++) {if(max<i[x]){ max=i[x]; month=x;} } return month; } public double minRain(double[] i) {double min=i[1]; int month=0; for (int x=0;x<12;x++){ if(min>i[x]){ min=i[x]; month=x; } } return min; } }
- 02-26-2013, 09:02 AM #2
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Code Error+ problems with inputting array lists into methods?
Hi Kazyu,
Yes, you code have problem with method. Because main method is static that is not call non-static method. you'll change non-static methods to static methods..like below..
Now your code is run successfully... Then you have made another mistake on your code for calculate the average method avgRain(rainfall). you passed array as a parameter. You created a method only used double variable like double avgRain(double total)..So will change the code like belowJava Code://Methods public static double totalRain(double[] i) {double sum=0; for (int x=0;x<12;x++){ sum+=i[x]; } return sum; } public static double avgRain(double total) {double avg=total/12; return avg; } public static double maxRain(double[] i) {double max=i[1]; int month=0; for (int x=0;x<12;x++) {if(max<i[x]){ max=i[x]; month=x;} } return month; } public static double minRain(double[] i) {double min=i[1]; int month=0; for (int x=0;x<12;x++){ if(min>i[x]){ min=i[x]; month=x; } } return min; }
Java Code:double sum=totalRain(rainfall); System.out.println("The avg rainfall is "+ avgRain(sum));Last edited by tamilarasi; 02-26-2013 at 09:04 AM.
Regards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
- 02-26-2013, 09:18 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
help inputting values into an array
By pds8475 in forum New To JavaReplies: 3Last Post: 01-22-2011, 05:45 PM -
Array Lists help!!
By lilika in forum New To JavaReplies: 12Last Post: 01-04-2011, 02:05 PM -
Problems With Array/Methods
By blueduiker in forum New To JavaReplies: 4Last Post: 01-19-2010, 01:49 AM -
Eclipse auto-complete lists duplicate methods
By Red Hunt in forum EclipseReplies: 1Last Post: 12-10-2009, 10:53 AM -
please i need the code of comparing these two array lists.
By raj reddy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 04-18-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks