The below problem comes from Chapter 5 of the book Intro to Java by Daniel Liang:
(Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.
Here is a sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
My code currently fulfills the requirements of the question but I'd like to cover all of my bases. With a very basic knowledge of Java, is there a way I can check the nextInt
in my while statement to see if:
The number series doesn't end with a 0. In my testing, the program just seems to stop (note my debugging output in my While statement). I am guessing there isn't a simple way to do this, thus we are to use 0 as a sentinel, but I figured I would ask.
If only the Enter key is pressed. When I do that in NetBeans the prompt moves to the next line, I would like to include a message notifying the user that nothing was entered and to enter an integer.
My Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package positivenegative;
/**
*
* @author
*/
import java.util.Scanner;
public class PositiveNegative {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
// Welcome message
System.out.println("Welcome to the Number Averagererer.");
// Yes I'm aware "Averagererer" isn't a word
System.out.println("Be sure to enter a 0 at the end");
System.out.println("or the program will not work.");
System.out.print("Enter an integer, the input ends if it is 0: ");
// Initialize variables
int number, totalPositive=0, totalNegative=0, counter=0;
double average=0;
// Process the entered variables and sort/calculate them
while ((number = input.nextInt()) != 0){ // Continues if number isn't 0
System.out.println(number); //debugging to see what the nextInt is doing
// Adds to positive
if (number > 0){
totalPositive++;
counter++;
average = (average + number);
}
// Adds to negative
else{
System.out.println(number);
totalNegative++;
counter++;
average = (average + number);
}
}
// Output message if the user only enters "0"
if (counter == 0){
System.out.println("No numbers were entered except 0");
}
// Output message with calculated data from input
else{
double total = average;
average = (average / counter);
System.out.println("The number of positives is: " + totalPositive);
System.out.println("The number of negatives is: " + totalNegative);
System.out.println("The total is: " + total);
System.out.println("The average is: " + average);
}
}
}