Results 1 to 12 of 12
- 12-02-2012, 12:38 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
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?
Here is the other class that I link it back to to do some calculation on 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 } } }
Thanks.Java Code:public class Rainfall { double avgRain; double rainfall[] = new double[11]; }
- 12-02-2012, 01:24 AM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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 )
- 12-02-2012, 01:44 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
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
- 12-02-2012, 02:00 AM #4
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
Re: How to store user input into an array
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'sJava 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 }
- 12-02-2012, 02:07 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
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.
- 12-02-2012, 02:13 AM #6
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
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
- 12-02-2012, 02:16 AM #7
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
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.
- 12-02-2012, 02:19 AM #8
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
Re: How to store user input into an array
sorry mate! my knowledge is heavily limited!
tried google? search like adding doubles to arrays
- 12-02-2012, 02:22 AM #9
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Re: How to store user input into an array
yeah I am thanks for all your help maybe someone else will post thanks again.
-
Re: How to store user input into an array
First off, your rainfall array should be initialized to 12 items, not 11 as you do here:
Next off, consider giving Rainfall a public method, say something like,Java Code:double rainfall[] = new double[11];
and then call this method inside of your for loop.Java Code:public void setRain(int month, double rainfall) { // code goes here to set the rainfall for the month passed in }
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.
- 12-02-2012, 03:55 AM #11
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Re: How to store user input into an array
Here's it updated
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: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()); } }
Also don't arrays start with 0 so it would be 0 - 11 which would actually be 12 values?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(){ } }
-
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:
Also, and again I recommend that your setRain method have two parameters as stated in my previous post.Java Code:rainfall[] = rain;
You can find a decent tutorial here: Arrays tutorial
Similar Threads
-
How to store user input from subclass into variables from superclass
By mc54321 in forum New To JavaReplies: 7Last Post: 11-04-2012, 11:04 PM -
User input to Array Question
By LukasHopkins in forum New To JavaReplies: 5Last Post: 08-14-2012, 01:12 PM -
I need help learning how to get user input and store a array
By justintime in forum New To JavaReplies: 1Last Post: 04-12-2012, 04:29 AM -
Creating 2D array from all user input
By peek_a_boo in forum New To JavaReplies: 1Last Post: 12-08-2011, 08:16 PM -
user input array
By localhost in forum New To JavaReplies: 5Last Post: 12-30-2010, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks