# Nested-If Problem/ Initializing

• March 2nd, 2011, 10:27 AM
ak120691
Nested-If Problem/ Initializing
So basically my code works (or at least I'm pretty sure it does). I removed the last if/else statements and it all runs. However when I put it in I get a "max" may not have been initialized.

Could someone please explain to me how to initialize it. I know the basics behind it. Apparently it's not giving the values to "max" which is preventing the program from compiling.

Here is the code:

Code Java:

```import java.util.Scanner; /** * MaxOfThree is a program that asks the user to input 3 different * integers, separated by a space and all on one line. * * It then checks if all 3 are different. If not, it informs user * about the error and terminates the program.   * Otherwise, it proceeds with computing the maximum of these 3 * integers and prints it. * * */   import java.util.Scanner; public class MaxOfThree { public static void main(String[] args) { // INITIALIZE YOUR SCANNER FOR GETTING INPUT FROM THE KEYBOARD Scanner keyboard = new Scanner(System.in);   // ASK THE USER TO TYPE IN THREE DIFFERENT INTEGERS.   System.out.print("Enter three different integers, all on one line.");   int max; // Maximum of i, j, and k. // Declare and read i, j, k.   int i; int j; int k;   i = keyboard.nextInt(); // ADD CODE HERE for reading j and k. // --> j = keyboard.nextInt(); k = keyboard.nextInt();   // Validate input by checking if any two are equal. // If yes, inform user of the error and // terminate by executing System.exit(0). // ADD CODE after if. HERE YOU MAY USE LOGICAL AND (&&) or OR (||). // -->   if ((i==j) || (i==k) || (k==j)) { System.out.println("Equal integers are not allowed! Exiting..."); System.exit(0); } else { System.out.println("You gave me legal numbers"); } // Since input is valid, proceed with determination of maximum. // Important: DO NOT USE LOGICAL AND (&&) or OR (||) IN THIS PART. // Just write nested else .. if ... else if .. statement. // This is just one long if - else - if - else.... statement.   // For example, if (i > j) // if ( i > k ) max = i;   // Complete the if-else statement // ADD CODE AFTER else. // -->     if (i > j) { if (i > k) { max = i; }   else { max = k; }   } if (j > i) { if (j > k) { max = j; }   else { max = k; }   } if (k > j) { if (k > i) { max = k; }   else { max = i; }   }   // Print max as shown in the sample session. // ADD CODE HERE. // --> System.out.println("Maximum of Three integers " + i + ", " + j + ", " + k + " is " + max);   } }```

Oh and I cannot use && and ||. I have to compare them with < > =, and if else statements.
• March 2nd, 2011, 10:51 AM
KevinWorkman
Re: Nested-If Problem/ Initializing
Basically, the problem is that Java needs a guarantee that a variable has been initialized before it can use it. So this won't compile:

Code java:

```int x;   if(something){ x = 1; }   System.out.println(x);```

Because if something does not evaluate to true, then there is no guarantee that x has been initialized, so there will be no way to do the last line. However, this is okay:

Code java:

```int x;   if(something){ x = 1; } else{ x = 2; }   System.out.println(x);```

Because either way, x will have been initialized. Another way to do that is this:

Code java:

```int x = 2;   if(something){ x = 1; }   System.out.println(x);```

But that only works if you have a logical default value for the variable.
• March 2nd, 2011, 04:16 PM
ak120691
Re: Nested-If Problem/ Initializing
I understand that. What confuses me is how to compare three variables without using the && and || operators.

I have never coded for 3 variables. Comparing 2 is simple. When it hits 3 you have to compare the first two and if it still doesn't work out then it'll assign it to whatever is left.

Could you please look at my code and help me fix the if / else / else if portion?

I removed that portion and just threw in a simple if/else with two variables and it compiled without a problem.
• March 2nd, 2011, 04:52 PM
KevinWorkman
Re: Nested-If Problem/ Initializing
Then I guess I don't understand your question. What exactly is the line you're having trouble with? What's the problem? Are you getting an exception? Something else?
• March 2nd, 2011, 04:57 PM
ak120691
Re: Nested-If Problem/ Initializing
bash-2.05\$ javac MaxOfThree.java
MaxOfThree.java:98: variable max might not have been initialized
System.out.println("Maximum of Three integers " + i + ", " + j + ", " + k + " is " + max);

That is my error. Apparently it won't give the values to max.

Can someone please fix it? I honestly have tried hours re-editing and re-coding it in all sorts of ways. It will not assign the greatest values to max.
• March 2nd, 2011, 07:21 PM
vanDarg
Re: Nested-If Problem/ Initializing
You never initialized the "max" variable. I'll try to explain it as easy as I can. To initialize means to set equal to a value, even if that value has the possibility of being changed 100 times over again, the variable still must have an initial value. Without initializing it, Java thinks that it is possible to compile and execute the code without the variable ever getting a value. Simply set it equal to zero when it is declared, and it should run fine.
• March 2nd, 2011, 07:37 PM
ak120691
Re: Nested-If Problem/ Initializing
... I love you lol. Thank you so much!!!