I'm getting an error saying local variable may have not been initialized, or cannot be resolved to a variable at where the catch is. How should I fix this? Basically, I want my program to accept a few numbers, then stop and display some answers.
import java.util.Scanner;
public class math2{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Integer Values\nEnter a Non-Integer When Finished\n");
int x = input.nextInt();
int max = x;
int min = x;
int sum = x;
double average = 0;
try
{
int amount = 1;
while(true)
{
x = Integer.parseInt(input.next());
sum = sum + x;
amount++;
average = (double)sum/amount;
if (x > max) {
max = x;
} else if (x < min) {
min = x;
}
}
}catch(NumberFormatException e){
System.out.println("The Maximum Value is " + max);
System.out.println("The Minimum Value Is " + min);
System.out.println("The Sum Is " + sum);
System.out.println("The Average Is " + average);}
}
}