0
import java.util.Scanner; 
public class Unit2Err2{ 
  public static void main( String[] args ){ 
    Scanner scan = new Scanner(System.in);                    
    double sum = 0;                                             
    double count = 0;
    double in = scan.nextDouble();                                                                      
    while (scan.nextDouble()!= 0){                                           
      sum = sum + in;                                           
      count++;                                  
    } 
    double avg= sum/count;                       
    System.out.println("The average is " +avg); 
  }                                                     
} 

Input: 5 4 3 0 6 4 3

In particular I don't have any errors, I did have a error which said: incomparable types: boolean and int. But I fixed it, my issue now is that the average should be 4 instead of 5. I'm wondering where the error is in this, I have tried rearranging it so my average comes out to 4 , but then I often end up with the same error as above.

2
  • Its because you are adding only the first number multiple times and not reassigning in with next scanned double value Commented Jun 3, 2016 at 3:10
  • double in = scan.nextDouble(); while (scan.nextDouble()!= 0){ Since Your are not reassigning The in is 5 and condition checks for next double so for input 5 4 3 0 6 4 3 here is the value that gets added for corresponding input in bracket 5 + 5(4) + 5(3) + 5(6) + 5(4) + 5(3) so its 30/6 hence your are getting 5. Commented Jun 3, 2016 at 3:35

3 Answers 3

1
import java.util.Scanner;
public class Unit2Err2{
public static void main( String[] args ){
    Scanner scan = new Scanner(System.in);
    double sum = 0;
    double count = 0;
    double in ;
    while ( (in = scan.nextDouble())!= 0){
        sum = sum + in;
        count++;
    }
    double avg= sum/count;
    System.out.println("The average is " +avg);
  }
}

when you first use nextDouble here double in = scan.nextDouble(); it has read a value from the terminal.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to re-assign in at each iteration:

while ((in = scan.nextDouble()) != 0){                                           
    sum += in;                                           
    count++;                                  
} 

Comments

0

The program stops and waits for input when it sees the "scan.nextDouble()" statement. Having two in a row is going to read both 5 and 4 without doing anything with the values.

The current code assigns the first input of 5 to the variable in. After that, in is never assigned the value. So 5 is being added each loop iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.