0

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);}
    }

}

2
  • Now everything else works except for when the user inputs a non integer as their first number. Commented Jul 30, 2013 at 7:22
  • I have updated my solution to resolve the issue please have look into Commented Jul 30, 2013 at 8:14

5 Answers 5

6

declare below variable before try block,So that these are available in catach block also.
and also initialize double average to some default value.

  String x = input.next();
  int y = Integer.parseInt(x);*emphasized text*
  int max = y;
  int min = y;
  int sum = 0;
  double average = 0;

Update

After your edit in question I noticed that you are getting InputMismatchException,
if you give non-integer as first input.
For this you can catch that Exception to exit normally from the program.
Replace this code in place of int x = input.nextInt(); statement of your code.

    int x = 0;
    try{
        x = input.nextInt();
    }catch(InputMismatchException ime){
       //you cam use either of one statemnet.I used return statement
        return ;
        //System.exit(0);
    }    
Sign up to request clarification or add additional context in comments.

Comments

2

Firstly , define the variables out side the try block. If you define it within try{}, it will be scoped inside the try{...} and invisible for catch(){} block.

Secondly , You need to provide some initial value to average . Local variables are never initialized with default values.

double average = Double.NaN;

What if the variable was never initialized inside the while() loop due to an Exception ?

2 Comments

A fast answer +1. I'd prefer NaN for cases where the value is really undefined though.
Well he should do but thats not the problem... The problem is, he has to declare the variables before the try
1

Because you haven't initialized the variable 'average'. Also, I would suggest you to declare the variables outside the try catch block

Comments

0

Declare max, min, average,sum variables before try block. Those variables are local to try block.. if you want to use at catch block make that variables as class members.

Comments

0
import java.util.Scanner;

public class math2{
static double average;
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");

    try
    {

    String x = input.next();
    int y = Integer.parseInt(x);
    int max = y;
    int min = y;
    int sum = 0;
    int amount = 0;


    while(true)
    {
        sum = sum + y;
        y = Integer.parseInt(input.next());

        amount++;
        average = (double)sum/amount;

            if (y > max) {
                max = y;
            } else if (y < min) {
                min = y;
            }
    }
    }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);}
    }

}

see you declare the variable at class level and compiler will assign it a default value for you ...but for local variables you need to assign it , as mention in other posts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.