I am writing a simple program to calculate the average of a set of numbers. You get the numbers using Scanner
, so I am using while
loop with .hasNext()
as a condition. However the loop is infinite.
Is it possible to break out of it without writing some certain word like "stop" in the input?
public class main {
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
int count = 0;
int temp;
int sum = 0;
while(Input.hasNextInt()){
count++;
temp = Input.nextInt();
sum += temp;
System.out.println(temp);
System.out.println(count);
} // End of while
double average = sum / count;
System.out.println("The average is: " + average);
} // End of method main
}