Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Help how do I get the position out of an array

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help how do I get the position out of an array

    Hey I have a few problems. When I perform calculations on my array it seems sometimes it works like for finding the total or average of inputting all 20's but other times it doesn't. Also I'm trying to set a variable equal to the position of the max and min number in the array. This isn't working I'm not sure if its initializing it or not. Thanks.

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

    This is the class with all the proplems
    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");
        }
     
        //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 ");
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    This isn't working
    Could you explain what "isn't working" means?
    Perhaps post some output from the program and add some comments to it describing what is wrong.

    Try debugging the code by adding some println statements that print out the values of the variables as they are given values and used.
    Print out the contents of the rainfall array first thing in the calc() method so you can see if the data is correct.
    Use the Arrays class's toString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    codyjava (December 2nd, 2012)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help how do I get the position out of an array

    Also at java-forums.org.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    codyjava (December 2nd, 2012)

  6. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Okay thanks for answering I did what you said and found that my array isn't storing the right data. My setRain() seems to just be setting the last value entered in it. So my current questions is what's wrong with that part of my code. Thanks

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    Can you explain what the setRain() method is supposed to?
    Then look at the code and see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    codyjava (December 2nd, 2012)

  9. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Yes, from the rainfallTester class in mainRain() I want it to store the user input into the double rain. From there I'm calling the object temp to send it to my Rainfall class (temp.setRain(rain)). Once in the Rainfall class I want it to populate the array. So the for loop will start a position 0 and the first number the user entered will be stored there. I think where its going wrong is the for loop. Once the user inputs a value it stores it but each time the user inputs a value it starts over so it actualy is only storing the last value. Is that right and if so how do I store the users input. Thanks.

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    Should the setRain() method store the passed value in ALL of the slots in the rainfall array
    or just in one slot in the array?

    If only one, then it needs to define the index to the rainfall array as a class variable, use it to save the value and then increment it for the next time setRain() is called.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    codyjava (December 2nd, 2012)

  12. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Yes I want it to store a value just in 1 slot. I'm not quit sure what you mean I'm sorry to bother but could you show me. Thanks this is a major help I think all would work if I got this right.

  13. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    Which part don't you understand?
    define the index to the rainfall array as a class variable,
    use it to save the value
    increment it for the next time
    If you don't understand my answer, don't ignore it, ask a question.

  14. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Mainly the define index as a class variable I'm more of a visual learner. If you could just type out the code for that sorry.

  15. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    There are three class variables in the Rainfall class. Add the definition of the index next to the array it will be used to index into.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Never mind I understand alright that part works, if you don't mind can you help me with the part where I take max value along with the min value and set their positions in the array to a variable.
    for (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 (i=0; i<rainfall.length;i++){
                if (min > rainfall[i]){
                    min = rainfall[i];
                    lmonth = i;
                }
            }

    I think where its going wrong is all of the if statements here.
    //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 ");
            }
        }
    }

    Thanks so much.

  17. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help how do I get the position out of an array

    the part where I take max value along with the min value and set their positions in the array to a variable
    Please explain what the code does now and what is wrong with that.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #14
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help how do I get the position out of an array

    Never mind I had hmonth instead of lmonth. Thanks again for all your help. I'm new when it comes to all this thanks.

Similar Threads

  1. how to position graphics
    By IHeartProgramming in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2012, 09:18 AM
  2. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  3. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM
  4. Search for min and max values as columns position in array
    By susieferrari in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 07:39 AM
  5. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM

Tags for this Thread