I have a code that accepts String as an input and parses it into a number. Here, whenever user input consists of elements that is not a number it will throw a NumberFormatException. However, I want to make it continuous,such as if program ran one time, it does not terminate but rather waits for further input.
public class ParseNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);// reading from standard input
String line = scan.nextLine(), word = null;
scan = new Scanner(line);
int sum = 0, count = 0;
while(scan.hasNextLine()){
word = scan.next();
sum += Integer.parseInt(word);
count++;
}
try{
if (count ==0)
System.out.println("There are no VALID input provided!");
else
System.out.printf("Sum = %d\nCount = %d\nAverage = %.3f\n", sum, count, (float) sum / count);
}catch(NumberFormatException e){
System.out.println(e.getMessage());
}
}
}