View Single Post
Old 12-02-2012, 01:32 AM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
How to add user input in an array

Hey I'm trying to have the user input a value and store it in an array. The only problem is it's saying I can't do it due to a lack of percision (double to int). I've looked and all my values are double. Am I doing it wrong. Thanks.



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:");
    temp.rainfall[j] = scan.nextDouble(); //this is giving me an error
        }
    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());
      }
    
}
Another question is how would I get values back such as the total do I use counter or something.

Code:
public class Rainfall {
    double avgRain;
    double sum = 0;
    
    double rainfall[] = new double[11];
    
     double getTotal(){
         
       return  sum += rainfall[];
     }
     
     double getAvg(){
         sum += rainfall[];
         return sum/rainfall.length;
     }
     
     double getMax(){
         
     }
     
     double getMin(){
         
     }
    
}
Thanks.
CodyJava is offline   Reply With Quote