Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 12-02-2012, 02:16 PM   PM User | #2
zergi
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 1
Thanked 2 Times in 2 Posts
zergi is an unknown quantity at this point
In the cycle use the int type for j instead of double.

Quote:
Another question is how would I get values back such as the total do I use counter or something.
Yes, you may use counter
Code:
double getTotal(){
  sum = 0;    
  for ( int i = 0; i <  rainfall.length; i++ ) {
    sum += rainfall[i];
  }      
  return  sum; 
}
zergi is offline   Reply With Quote
Users who have thanked zergi for this post:
CodyJava (12-02-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:05 AM.


Advertisement
Log in to turn off these ads.