Briar Lostlen
Briar Lostlen

Reputation: 1

Java: How to infinitely enter numbers, stop when negative number is entered and computer the average

I am trying to write a Java program that takes in a potentially infinite number of values - and once the user enters a negative number, the program stops, computes the average of all of the entered numbers (excluding the negative one) and prints out how many numbers were entered (once again, not the negative one) as well as the average.

Below is the code I currently have. When I try to run the program, it does not computer the average correctly and you have to enter a couple consecutive negative numbers for it to finally stop the program.

To test the arithmetic and the rest of the program, I inserted a statement that would close the program if the word "negative" was entered rather than a negative number. When did this, the average and count and everything else worked just like it was made to. Essentially, the problems start to occur when I try to stop the program after a negative number.

I am a beginning programmer and this has been driving me crazy for a couple hours. Any help is greatly appreciated!

import java.util.*;
import java.lang.Math;
import java.io.IOException;
import java.io.InputStream;

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

        double avg = 0.0;
        double count = 0.0;
        double sum = 0.0;

        System.out.println("Enter a series of numbers. Enter a negative number to quit.");

        while (numInput.hasNextDouble())
        {
            double negNum = numInput.nextDouble();
            if (negNum >= 0)
            {
                sum += numInput.nextDouble();
                count++;
                avg = sum/count;
            }
            else 
            {
                System.out.println("You entered " + count + " numbers averaging " + avg + ".");
                break;
            }
        }
    }
}

Upvotes: 0

Views: 3861

Answers (4)

Titus
Titus

Reputation: 22474

You should use: sum+=negNum; instead of sum += numInput.nextDouble();

As it is now, your program, reads a number and if it is not negative it reads another number and adds it to the sum.

Also, you should compute the average only once in the else block.

Upvotes: 1

Naman Gala
Naman Gala

Reputation: 4692

Change

sum += numInput.nextDouble(); // reading next value again

to

sum += negNum;

Upvotes: 0

akhil_mittal
akhil_mittal

Reputation: 24157

You have already received the number entered by the user in line:

double negNum = numInput.nextDouble();

You should add this number itself to sum rather than asking for another number from user by calling numInput.nextDouble() again. So the fixed code would be:

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

        double avg = 0.0;
        double count = 0.0;
        double sum = 0.0;

        System.out.println("Enter a series of numbers. Enter a negative number to quit.");

        while (numInput.hasNextDouble())
        {
            double negNum = numInput.nextDouble();
            if (negNum >= 0)
            {
                sum += negNum;
                count++;
                avg = sum/count;
            }
            else
            {
                System.out.println("You entered " + count + " numbers averaging " + avg + ".");
                break;
            }
        }
    }

Sample Run:

Enter a series of numbers. Enter a negative number to quit. 2 3 -1 You entered 2.0 numbers averaging 2.5.

Upvotes: 0

Codebender
Codebender

Reputation: 14471

You are reading a new number to compute the sum.

It should be

sum += negNum;

Upvotes: 0

Related Questions