Amani Lama
Amani Lama

Reputation: 305

When i introduce a while loop I get error message of variable may not be initialized... why is that?

I have a main that calls a method 1 that calls a method 2:

    public class azza_wahada_A3Q1 {
           public static void main (String[] args) {
           Method1 m1 = new Method1();


      int age = m1.getValidNumber("Please enter your age.", 0, 110);

    //int age = m2.getInteger("Please enter your age.");
    System.out.println("u r age is \n"+age);
    }
    }

public class Method1 {

public static int getValidNumber(String prompt, int min, int max){
   int input;
   Method2 m2 = new Method2();
   Method3 m3 = new Method3();
   Boolean range = false;

   while(range){
        input = m2.getInteger(prompt);

        if (input > min && input < max){
            range = true;
           // return input;
        }
        else{
            m3.showError(input,min, max);
            range = false;
        }

   }
   return input;
  }
}
import javax.swing.JOptionPane;
public class Method2 {
  public static int getInteger(String prompt){

  String message;
  int getInt;

  message = JOptionPane.showInputDialog(null, prompt);
  getInt =  Integer.parseInt(message);
  return getInt ;
}

}
import javax.swing.JOptionPane;
public class Method3 {
  public static void showError(int number, int min, int max){

 String error_message;
 error_message = JOptionPane.showInputDialog(null, "Please enter a new number");

}

}

Why does that happen? the code works fine without the while loop, when I introduce the loop I get error message saying that my input variable may not have been intialized, shows the error at the return input in method 1. What is going on? Thank you

Upvotes: 0

Views: 176

Answers (6)

vivekj011
vivekj011

Reputation: 1149

You cant declare variables in a method(in Java).

When you declare any local/block/method variable, they do not get the default value.

You must assign some value before accessing it other wise compiler will throw an error.

So your solution is : since you are using int input, replace it with int input = 0;

More & quick info : http://anotherjavaduke.wordpress.com/2012/03/25/variable-initialization-and-default-values/

Upvotes: 1

Ingo
Ingo

Reputation: 36339

 the code works fine without the while loop, 

Sure, because the first you are doing is:

input = m2.getInteger(prompt);

thus initializing input. But, in fact, when wrapping this in a while loop, this might not execute. As it stands, it will not execute, as the condition is false.

Upvotes: 0

gefei
gefei

Reputation: 19776

with the while loop, it is theoretically possible that the while loop does not get executed, that is, when the condition range simply values to false. the compiler does not know if the loop will be executed, and therefore it thinks it is possible that the varialbe input does not get initialized.

Upvotes: 2

Maroun
Maroun

Reputation: 95968

You need to change range to be true, and set it to false inside the while loop.

In your code, you're never entering the while loop, thus, the variable is never initialized.

This causes input to never be initialized. Change it and it should work.

Upvotes: 0

Richard Sitze
Richard Sitze

Reputation: 8463

Note that your control variable is range = false, so in fact the body of that loop never executes and input is not initialized.

Upvotes: 0

Ajay S
Ajay S

Reputation: 48602

You need to initialize the local variable at the time of declaration.

int input = 0;

Upvotes: 0

Related Questions