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

    Default How to store user input into an array

    Hey I'm trying to have the user input the total rainfall for 12 months. I have a for loop which should run for 12 times (if its wrong let me know), and each time it runs I want it to ask the user to input a value. My main question is how do I store that value in an array, but the array is in another class. I thought i would use [i] doesn't that add values to it?

    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(int j=0; j<temp.rainfall.length;j++ ){
        System.out.println("What is the total rainfall for the month:");
        temp.rainfall[] = scan.nextDouble();  //this is where the problem is 
            }
        
          }
        
    }
    Here is the other class that I link it back to to do some calculation on it.
    Java Code:
    public class Rainfall {
        double avgRain;
        
        double rainfall[] = new double[11];
        
         
        
    }
    Thanks.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: How to store user input into an array

    Hi!

    Arrays in Java have fixed sizes.
    I would use ArrayList if I were you. You can simply create a method in your class Rainfall that calls the list.add(index, your element).

    Look at the API,
    and check out examples at Stackoverflow etc.

    ArrayList (Java Platform SE 7 )

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

    Default Re: How to store user input into an array

    So an array list is different than an Array its an assignment though and we were told to use an array. My main question is though how to get the user input value into the array. Thanks

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: How to store user input into an array

    Java Code:
        public static void main(String args[])
        {
            double[] a=new double[6];
            Scanner scan=new Scanner(System.in);
            System.out.println("Please enter double w/e");
            for(double x=0;x<6;x++)
                a[x]=scan.nextDouble();
            
    //then you could print the elements ill let you figure that
        }
    does this help? is this what you mean, sorry if that doesnt work, coded without running because im on a stupid network and pc and it hates running IDE's

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

    Default Re: How to store user input into an array

    I have to change the for(double j;j<....) thing to int j. It wouldnt let me put j in the array if it was a double (loss of pecision) so I changed it and then it complied with errors but still kindaa worked. I think it was because I'm scanning for a double though. Any ideas. Thanks.

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: How to store user input into an array

    why do you need it to be int j?

    i havent worked with arrays for a while but isnt that saying for each element of double, start at 0 go to <6 each time ++?

    it will get an error about accuracy if you try force int and double.

    sorry im so out of practice with arrays :L

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

    Default Re: How to store user input into an array

    It's okay but yeah I tried to put double and when i put j into the array it gave me an error I don't know why. I want it to be double.

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: How to store user input into an array

    sorry mate! my knowledge is heavily limited!

    tried google? search like adding doubles to arrays

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

    Default Re: How to store user input into an array

    yeah I am thanks for all your help maybe someone else will post thanks again.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to store user input into an array

    Quote Originally Posted by codyjava View Post
    Hey I'm trying to have the user input the total rainfall for 12 months. I have a for loop which should run for 12 times (if its wrong let me know), and each time it runs I want it to ask the user to input a value. My main question is how do I store that value in an array, but the array is in another class. I thought i would use [i] doesn't that add values to it?

    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(int j=0; j<temp.rainfall.length;j++ ){
        System.out.println("What is the total rainfall for the month:");
        temp.rainfall[] = scan.nextDouble();  //this is where the problem is 
            }
        
          }
        
    }
    Here is the other class that I link it back to to do some calculation on it.
    Java Code:
    public class Rainfall {
        double avgRain;
        
        double rainfall[] = new double[11];
        
         
        
    }
    Thanks.
    First off, your rainfall array should be initialized to 12 items, not 11 as you do here:
    Java Code:
     double rainfall[] = new double[11];
    Next off, consider giving Rainfall a public method, say something like,

    Java Code:
    public void setRain(int month, double rainfall) {
        // code goes here to set the rainfall for the month passed in
    }
    and then call this method inside of your for loop.

    Also, if you get error with your code and need help, please post the latest code, the exact error message, and indicate which line in the code is causing the error. This is the best way to get us to understand the error and possibly help you.

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

    Default Re: How to store user input into an array

    Here's it updated
    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.println("What is the total rainfall for the month:");
        rain = scan.nextDouble();
        temp.setRain(rain);
            }
        System.out.println("The total rainfall for the year is "+temp.getTotal());
        
        System.out.println("The average monthly rainfall is "+temp.getAvg());
        
        System.out.println("The month with the most rainfall is "+temp.getMax());
                
        System.out.println("The month with the least rainfall is "+temp.getMin());
          }
        
    }
    I'm sending the value entered to the constructor setRain. Now I need to put those values into the array how do i do that?

    Java Code:
    public class Rainfall {
        double avgRain;
        double sum = 0;
        
        double rainfall[] = new double[11];
        
        public void setRain(double rain){
            rainfall[] = rain;
        }
        
         double getTotal(){
             
           return  sum += rainfall[];
         }
         
         double getAvg(){
             sum += rainfall[];
             return sum/rainfall.length;
         }
         
         double getMax(){
             
         }
         
         double getMin(){
             
         }
        
    }
    Also don't arrays start with 0 so it would be 0 - 11 which would actually be 12 values?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to store user input into an array

    You will want to read a tutorial on use of arrays as this is not correct, and I'm sure that the compiler will tell you so:

    Java Code:
    rainfall[] = rain;
    Also, and again I recommend that your setRain method have two parameters as stated in my previous post.

    You can find a decent tutorial here: Arrays tutorial

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2012, 11:04 PM
  2. User input to Array Question
    By LukasHopkins in forum New To Java
    Replies: 5
    Last Post: 08-14-2012, 01:12 PM
  3. Replies: 1
    Last Post: 04-12-2012, 04:29 AM
  4. Creating 2D array from all user input
    By peek_a_boo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 08:16 PM
  5. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •