Lish
Lish

Reputation: 231

User input only occurs once or never stops during loop

I am attempting a program that reads an unspecified number of integers, find sum, positive, negative and average. My issue is that either it will only run and allow one integer to be typed and then do nothing, or with the following code, it never stops letting you enter numbers therefore I can't get past. I have the number = 0 output correct.

public class Compute {

// Count positive and negative numbers and compute the average of numbers
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int sum = 0;
        positive = 0;
        negative = 0;
        total = 0;

        System.out.println("Enter an integer, the input ends if it is 0: ");
        int numbers = input.nextInt();

        do {
            if (numbers > 0) {
                positive++;//add 1 to positive count
            } else if (numbers < 0) {
                negative++;//add 1 to negative count
            }//end else if

            sum += numbers; //add integer input to sum

            numbers = input.nextInt();
            total++;
        } while (numbers != 0);

        if (numbers == 0) {
            System.out.println("No numbers are entered except " + numbers);
        }//end if
    }
}

Upvotes: 0

Views: 1479

Answers (2)

gerrytan
gerrytan

Reputation: 41123

Following code snippet should give you a good example on how to read integers from console:

Scanner scanner = new Scanner(System.in);
do {
  int i = scanner.nextInt();
  // ...
} while (scanner.hasNext());

The scanner.hasNext() method call will block until user enter the next number on console

Upvotes: 1

Rais Alam
Rais Alam

Reputation: 7016

Try below code. to terminate the loop and see output type 0 as input at any time of execution.

import java.util.Scanner;

public class Compute {

    // Count positive and negative numbers and compute the average of numbers
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int sum = 0;
        int positive = 0;
        int negative = 0;
        int total = 0;

        System.out.println("Enter an integer, the input ends if it is 0: ");
        int numbers = input.nextInt();

        do {

            if (numbers > 0) {
                positive++;// add 1 to positive count
                sum += numbers; // add integer input to sum
            }

            else if (numbers < 0) {
                negative++;// add 1 to negative count
                sum -= numbers; // add integer input to sum
            }

            numbers = input.nextInt();
            total++;

        } while (numbers != 0);

        System.out.println("The number of positives is \t " + positive);
        System.out.println("The number of negatives is \t " + negative);
        System.out.println("The total count of number is \t " + total);
        System.out.println("The sum of all number is    \t" + sum);
        System.out.println("The average is           \t"
                + ((double) sum / (positive + negative)));

    }// end main
}// end Compute

Upvotes: 1

Related Questions