# Is there an easier way to get position out of array

• 12-02-2012, 05:27 PM
codyjava
Is there an easier way to get position out of array
Hey so I'm suppose to get the user to input the total rainfall for all the months and then it will display the total. Also what month had the most and least rainfall. My question is, is there an easier way to do it basiclly I'm setting each position in the array (say rainfall[1] = 42) then it would run a loop and when a variable equals 1 it would display February.

Code:

```import java.util.Scanner; public class RainfallTester {     public static void main(String [] args){     Scanner scan = new Scanner(System.in);     Rainfall temp = new Rainfall();     double rain;         for(double j=0; j<temp.rainfall.length;j++ ){     System.out.print("What is the total rainfall for the month:");     rain = scan.nextDouble();     temp.setRain(rain);         }     System.out.println("The total rainfall is "+temp.totalRain());         System.out.println("The average rainfall is "+temp.avgRain());         System.out.println("The month with the most rainfall is "+temp.mostRain());         System.out.println("The month with the least rainfall is "+temp.leastRain());       }     }```

Here's the part where it decides the month.
Code:

```public class Rainfall {     double avg;     double sum;     int hmonth = 0;     int lmonth = 0;         double rainfall[] = new double[12];         public void setRain(double rain){         for(int i =0;i<rainfall.length;i++){           rainfall[i] = rain;         }             }     double totalRain(){         for(int i =0; i<rainfall.length;i++){         sum += rainfall[i];         }       return sum;     }     double avgRain(){         for(int i =0; i<rainfall.length;i++){         sum += rainfall[i];         }         avg = sum/rainfall.length;         return avg;     }     double mostRain(){         double max = 0;                 for (int i=0; i<rainfall.length;i++){             if (max < rainfall[i]){                 max = rainfall[i];                 hmonth = i;             }         }         return max;     }     public void mostMonth(){         if(hmonth == 1){             System.out.print("January ");         }else if(hmonth == 2){             System.out.print("Feburary ");         }else if(hmonth == 3){             System.out.print("March ");         }else if(hmonth == 4){             System.out.print("April ");         }else if(hmonth == 5){             System.out.print("May ");         }else if(hmonth == 6){             System.out.print("June ");         }else if(hmonth == 7){             System.out.print("July ");         }else if(hmonth == 8){             System.out.print("August ");         }else if(hmonth == 9){             System.out.print("September ");         }else if(hmonth == 10){             System.out.print("October ");         }else if(hmonth == 11){             System.out.print("November ");         }else{             System.out.print("December ");         }     }         double leastRain(){         double min = 0;         for (int i=0; i<rainfall.length;i++){             if (min > rainfall[i]){                 min = rainfall[i];             }         }         return min;     }     public void leastMonth(){         if(hmonth == 1){             System.out.print("January ");         }else if(hmonth == 2){             System.out.print("Feburary ");         }else if(hmonth == 3){             System.out.print("March ");         }else if(hmonth == 4){             System.out.print("April ");         }else if(hmonth == 5){             System.out.print("May ");         }else if(hmonth == 6){             System.out.print("June ");         }else if(hmonth == 7){             System.out.print("July ");         }else if(hmonth == 8){             System.out.print("August ");         }else if(hmonth == 9){             System.out.print("September ");         }else if(hmonth == 10){             System.out.print("October ");         }else if(hmonth == 11){             System.out.print("Novenmber ");         }else{             System.out.print("December ");         }     } }```
Thank. Also my instructions were to just have the user input the rainfall not the month too.
• 12-02-2012, 08:50 PM
codyjava
Re: Is there an easier way to get position out of array
Here is an update to where I am.When I run it everything is fine it gives the the highest and lowest values but it wont give me there position. Say 10 was the lowest number in position 3, well I set 3 to April but it always gives me december why?? Thanks.

Code:

```public class Rainfall {     int hmonth = 0;     int lmonth = 0;         double rainfall[] = new double[12];         public void setRain(double rain){         for(int i =0;i<rainfall.length;i++){           rainfall[i] = rain;         }             }     public void calc(){         double sum =0;         double avg = 0;         double max = 0;         double min = rainfall[0];                 //find total         for(int i =0; i<rainfall.length;i++){         sum += rainfall[i];         }         System.out.println("The total rainfall is "+sum);                 //find average         avg = sum/rainfall.length;         System.out.println("The average rain fall is "+avg);                 //find max         for (int i=0; i<rainfall.length;i++){             if (max < rainfall[i]){                 max = rainfall[i];                 hmonth = i;             }         }         System.out.println("The month with the most rain is ");         mostMonth();         System.out.println("with "+max+" rain");         //find min         for (int i=0; i<rainfall.length;i++){             if (min > rainfall[i]){                 min = rainfall[i];                 lmonth = i;             }         }         System.out.println("The month with the least rain is ");         leastMonth();         System.out.println("with "+min+" rain");     }     //find most month     public void mostMonth(){         if(hmonth == 1){             System.out.print("January ");         }else if(hmonth == 2){             System.out.print("Feburary ");         }else if(hmonth == 3){             System.out.print("March ");         }else if(hmonth == 4){             System.out.print("April ");         }else if(hmonth == 5){             System.out.print("May ");         }else if(hmonth == 6){             System.out.print("June ");         }else if(hmonth == 7){             System.out.print("July ");         }else if(hmonth == 8){             System.out.print("August ");         }else if(hmonth == 9){             System.out.print("September ");         }else if(hmonth == 10){             System.out.print("October ");         }else if(hmonth == 11){             System.out.print("November ");         }else{             System.out.print("December ");         }     }         //find least month     public void leastMonth(){         if(hmonth == 1){             System.out.print("January ");         }else if(hmonth == 2){             System.out.print("Feburary ");         }else if(hmonth == 3){             System.out.print("March ");         }else if(hmonth == 4){             System.out.print("April ");         }else if(hmonth == 5){             System.out.print("May ");         }else if(hmonth == 6){             System.out.print("June ");         }else if(hmonth == 7){             System.out.print("July ");         }else if(hmonth == 8){             System.out.print("August ");         }else if(hmonth == 9){             System.out.print("September ");         }else if(hmonth == 10){             System.out.print("October ");         }else if(hmonth == 11){             System.out.print("Novenmber ");         }else{             System.out.print("December ");         }     } }```
The part that initializes it all.
Code:

```import java.util.Scanner; public class RainfallTester {     public static void main(String [] args){     mainRain();       }         public static void mainRain(){         Scanner scan = new Scanner(System.in);     Rainfall temp = new Rainfall();     double rain;         for(double j=0; j<temp.rainfall.length;j++ ){     System.out.print("What is the total rainfall for the month:");     rain = scan.nextDouble();     if(rain <= 0){           System.out.println("Error enter a non negative number.");           mainRain();       }         temp.setRain(rain);     }       temp.calc();   }     }```
• 12-02-2012, 09:41 PM
pbrockway2
Re: Is there an easier way to get position out of array
A small point, but mostMonth() does not "find most month" as the comment states. What it does is "prints a the string name of the greatest month followed by a space". It's as well to be precise - especially when things go wrong and you are trying to see whether each part of the program does what you intend.

In that vein, what is setRain() supposed to do?

---

You can check the data by printing it out (to see if you are working with numbers you think you are working with):

Code:

```public void calc(){     System.out.println("About to do calculation:");     for(int i = 0; i < rainfall.length; i++) {         System.out.println("i=" + i + "  rainfall[i]=" + rainfall[i]);     }     double sum =0;     double avg = 0;     double max = 0;     double min = rainfall[0];         // etc```
• 12-02-2012, 09:50 PM
pbrockway2
Re: Is there an easier way to get position out of array
Also at www.javaprogrammingforums.com.

@OP: If you are going to start a discussion at multiple places, please post links at each to the others so that everyone taking part knows what else is being said.

Also bear in mind that many people won't bother with discussions that are cross posted: they fear wasting their time saying something that has already been said, or saying something that loses its relevance when the discussion has moved on (at some other location) in a different direction. Some will respond to crossposts, some won't. In either case, though, it's just politeness that everyone should know what's going on, and where.
• 12-02-2012, 09:57 PM
codyjava
Re: Is there an easier way to get position out of array
Okay I wasn't having any luck here thanks.
• 12-02-2012, 10:05 PM
codyjava
Re: Is there an easier way to get position out of array
Okay I used this code and found that all my array is taking in is the value 20
Code:

```for(int i = 0; i < rainfall.length; i++) {         System.out.println("i=" + i + "  rainfall[i]=" + rainfall[i]);     }```
Ill re-post the codes
Code:

```public class Rainfall {     int hmonth = 0;     int lmonth = 0;         double rainfall[] = new double[12];         public void setRain(double rain){         for(int i =0;i<rainfall.length;i++){           rainfall[i] = rain;         }             }     public void calc(){         double sum =0;         double avg = 0;         double max = rainfall[0];         double min = rainfall[0];                 //find total         for(int i =0; i<rainfall.length;i++){         sum += rainfall[i];         }         System.out.println("The total rainfall is "+sum);                 //find average         avg = sum/rainfall.length;         System.out.println("The average rain fall is "+avg);                 //find max         for (int i=0; i<rainfall.length;i++){             if (max < rainfall[i]){                 max = rainfall[i];                 hmonth = i;             }         }         System.out.print("The month with the most rain is ");         mostMonth();         System.out.println("with "+max+" rain");         //find min         for (int i=0; i<rainfall.length;i++){             if (min > rainfall[i]){                 min = rainfall[i];                 lmonth = i;             }         }         System.out.print("The month with the least rain is ");         leastMonth();         System.out.println("with "+min+" rain");                 for(int i = 0; i < rainfall.length; i++) {         System.out.println("i=" + i + "  rainfall[i]=" + rainfall[i]);     }     }     //find most month     public void mostMonth(){         if(hmonth == 0){             System.out.print("January ");         }else if(hmonth == 1){             System.out.print("Feburary ");         }else if(hmonth == 2){             System.out.print("March ");         }else if(hmonth == 3){             System.out.print("April ");         }else if(hmonth == 4){             System.out.print("May ");         }else if(hmonth == 5){             System.out.print("June ");         }else if(hmonth == 6){             System.out.print("July ");         }else if(hmonth == 7){             System.out.print("August ");         }else if(hmonth == 8){             System.out.print("September ");         }else if(hmonth == 9){             System.out.print("October ");         }else if(hmonth == 10){             System.out.print("November ");         }else{             System.out.print("December ");         }     }         //find least month     public void leastMonth(){         if(hmonth == 1){             System.out.print("January ");         }else if(hmonth == 2){             System.out.print("Feburary ");         }else if(hmonth == 3){             System.out.print("March ");         }else if(hmonth == 4){             System.out.print("April ");         }else if(hmonth == 5){             System.out.print("May ");         }else if(hmonth == 6){             System.out.print("June ");         }else if(hmonth == 7){             System.out.print("July ");         }else if(hmonth == 8){             System.out.print("August ");         }else if(hmonth == 9){             System.out.print("September ");         }else if(hmonth == 10){             System.out.print("October ");         }else if(hmonth == 11){             System.out.print("Novenmber ");         }else{             System.out.print("December ");         }     } }```
Code:

```import java.util.Scanner; public class RainfallTester {     public static void main(String [] args){     mainRain();       }         public static void mainRain(){         Scanner scan = new Scanner(System.in);     Rainfall temp = new Rainfall();     double rain;         for(double j=0; j<temp.rainfall.length;j++ ){     System.out.print("What is the total rainfall for the month:");     rain = scan.nextDouble();     if(rain <= 0){           System.out.println("Error enter a non negative number.");           mainRain();       }         temp.setRain(rain);     }       temp.calc();   }     }```
Right now I basically want to know how to get the user input value into the array. What I'm doing isn't working thanks.