Results 1 to 6 of 6
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

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

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

  2. #2
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

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

    Java 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.
    Java 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();
       }
        
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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):

    Java 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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

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

  5. #5
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Is there an easier way to get position out of array

    Okay I wasn't having any luck here thanks.

  6. #6
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Java Code:
    for(int i = 0; i < rainfall.length; i++) {
            System.out.println("i=" + i + "   rainfall[i]=" + rainfall[i]);
        }
    Ill re-post the codes
    Java 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 ");
            }
        }
    }
    Java 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.

Similar Threads

  1. String array to position integers
    By azaleah in forum New To Java
    Replies: 4
    Last Post: 06-28-2012, 09:33 PM
  2. return position in array
    By forms in forum New To Java
    Replies: 3
    Last Post: 11-19-2011, 10:08 PM
  3. Get Characted at Array Position?
    By doa24uk in forum New To Java
    Replies: 3
    Last Post: 04-20-2011, 08:33 AM
  4. hope there is an easier way.
    By IYIaster in forum New To Java
    Replies: 6
    Last Post: 10-14-2009, 08:26 AM
  5. exchange the position of array elements
    By hacikho in forum New To Java
    Replies: 6
    Last Post: 09-20-2009, 11:02 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
  •