简体   繁体   中英

Why am I getting Output was not found error in Java?

I'm learning java and can't figure out why I keep getting this error. When I run program and type in 0 is outputs NaN.

Exercise: Write a program that asks the user for input until the user inputs 0. After this, the program prints the average of the positive numbers (numbers that are greater than zero). If no positive number is inputted, the program prints "Cannot calculate the average".

What I wrote:

import java.util.Scanner;

public class AverageOfPositiveNumbers {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        
        double input = 0;
        double sum = 0;
       
        while (true) {
            double userInput = Double.valueOf(scanner.nextLine());            
            
            if (userInput > 0) {
                input = input + 1;
                sum = sum + userInput;
            } 
            
            if (userInput == 0) {
                break;
            } 

            if (userInput < 0) {
                System.out.println("Cannot calculate the average");
            }
        }   
        System.out.println(sum / input);
    }
}

error:

When input was:
0
, the expected out put was:
nnot
Output was not found.

You are getting a NaN (Not a Number) error because your variables double input = 0;

and double sum = 0; are set to 0.

If you enter 0 as the first number, your program is dividing 0 by 0 which is not possible.

Here is my solution to this problem:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);


    double input = 0;
    double sum = 0;

    while (true) {
        double userInput = Double.parseDouble(scanner.nextLine());

        if (userInput == 0) {
            if (sum == 0) {
                System.out.println("You cannot enter 0 as the first number");
            } else {
                break;
            }
        }

        if (userInput > 0) {
            input++;
            sum = sum + userInput;
        }

        if (userInput < 0) {
            System.out.println("Cannot calculate the average");
        }

    }
    System.out.println(sum / input);
}
}

You're getting NaN(Not a Number). It's not an error, it's a special value. When you trying to divide double type zero by another double type zero you will always get NaN. That is happening, because you assign double input = 0 and double sum = 0, then the while{} loop breaks when user's first input is zero, then your program divides sum by input(0 by 0) and that's it - NaN.

As mentioned by others, you are attempting to divide by zero. This will throw an exception which you do not catch. You should make sure you don't divide by zero.

Also, since you have this inside the loop:

if (userInput < 0) {
    System.out.println("Cannot calculate the average");
}

It will print every time the user enters a negative number but it should only print at the end, if applicable.

Scanner scanner = new Scanner(System.in);

int input = 0;
double sum = 0;
       
while (scanner.hasNextDouble()) {
    double userInput = scanner.nextDouble();            
    // Only add to sum if > 0
    if (userInput > 0) {
        input += 1;
        sum += userInput;
    } 
    else if (userInput == 0) {
        break;
    }
    // Ignore negative numbers
}

// If input > 0 there was at least one positive number
if (input > 0) {
    System.out.println(sum / input);
}
else {
    System.out.println("Cannot calculate the average");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM