-2

I'm writing a simple simulation as part of my study (yes, I'm just beginner in Java). In it I'm taking input from user via Scanner and I want to check that input for valid format (only numbers) and valid range. I faced difficulties - input value does not come out of "while" loop. Could you please give me a tip? I'll appreciate it!

Here is part of my code you might be interested in:

// check for invalid input
String input;
boolean valid = false;
while (!valid){
    try{
        input = sc.next();
        user = Integer.parseInt(input);
        valid = true;
    }catch (NumberFormatException ex) {
        System.out.println(ex);
        System.out.println("Number but not a character or a symbol!");
    }
    System.out.println("Try once more!");
}
// interaction with user;
while (user !=3){
    if (user ==1){
        System.out.println("What angle I should set?");
        userChange = sc.nextDouble();
        shot.userAngle(userChange);
        shot.show();
    }
    else if (user ==2){
        System.out.println("What speed I should set?");
        userChange = sc.nextDouble();
        shot.userSpeed(userChange);
        shot.show();
    }
    else {
        System.out.println("Wrong number!");
    }
    System.out.println("What you want to change?");
    user = sc.nextInt();
}

It says that variable "user" might not be set.

Piyin
  • 1,823
  • 1
  • 16
  • 23
Max
  • 23
  • 4
  • 2
    Local variables need to be initialized. You are probably declaring `int user;` somewhere above, and not initializing it. You need to make it `int user = 0;` or something – Neil Locketz Oct 03 '17 at 20:39
  • Can you post the part of the code where you actually set user. – user3750325 Oct 03 '17 at 20:41
  • The compiler cannot tell that `user` must be set if the first `while` loop has exited. That is complex logic that is beyond the compiler's reasoning. – AJNeufeld Oct 03 '17 at 20:41
  • 3
    Possible duplicate of [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Juan Carlos Mendoza Oct 03 '17 at 20:41

1 Answers1

0

Soled. Working code here

    int user; 
    double userChange;
    boolean stop = false;

    // check for invalid input if not integer and if out of range
    // interaction with user
    while (!stop){
        try {
            System.out.println("What you want to change?");
            user = sc.nextInt();
            if (user ==3){
                    stop = true;
                }

            while (user !=3){
                if (user ==1){
                    System.out.println("What angle I should set?");
                    userChange = sc.nextDouble();
                    shot.userAngle(userChange);
                    shot.show();
                }
                else if (user ==2){
                    System.out.println("What speed I should set?");
                    userChange = sc.nextDouble();
                    shot.userSpeed(userChange);
                    shot.show();
                }

                else {
                    System.out.println("Wrong number!");
                }
                System.out.println("What you want to change?");
                user = sc.nextInt();
                if (user ==3){
                    stop = true;
                }
            }
        }
        catch (InputMismatchException e){
            System.out.println(e);
            System.out.println("Wrong input! Number but not a symbol or a character!");
            sc.next();

        }
    }
Max
  • 23
  • 4