# Help with initialize int variable without assigning a number?

• 11-23-2010, 01:39 PM
equal
Help with initialize int variable without assigning a number?
Hello! I wonder if it is possible to initialize a variable without giving it an value?
The part of my program I'm having trouble with:

Code:

```//*The program will ask for 10 ints, when 10 ints have been taken in from //input the software will print the 2 biggest and the 2 smallest int. I've got it //to work if i assign hels1,hels2,helm1,helm2 with a value. But if I do that //and the lowest number taken from input is higher the value given to the // variable the software looses its function...  Does anyone have any ideas? //Thanks in advace! for (int antHel = 0, hels1, hels2, helm1, helm2; antHel <= 10; antHel++ )  {                System.out.println("Skriv in ett heltal: ");                         //check the variable that is given from scanner and checks if to replace..                 int i = sc.nextInt();                 if (i >= hels1)                         hels1 = i;                                                 else if(i < hels1 && i <= hels2)                                 hels2 = i;                                                                 else if(i >= helm1)                         helm1 = i;                                 else if(i <= helm2)                         helm2 = i;                                 System.out.println(hels1);         System.out.println(hels2);         System.out.println(helm1);         System.out.println(helm2);                                                                         }                                                                 }         }```
• 11-23-2010, 02:21 PM
One way to handle the initialization is to use a "first time" initializer. Basically when you get the first number initialize your min and max variables to equal the first number entered. Here is an example:

Code:

```        int iteration, i, min, max;         Boolean isFirstTime = true;         Boolean isInputGood = true;         Scanner in = new Scanner(System.in);         i = min = max = 0;         iteration = 0;         while (iteration < 10)         {             System.out.print("Enter a number: ");             try             {                 i = in.nextInt();                 isInputGood = true;             }             catch (Exception e)             {                 System.out.println("EXCEPTION! " + e.toString());                 isInputGood = false;                 // or whatever exception handling you want . . .             }             if (isInputGood)             {                 iteration++;                                 if (isFirstTime)                 {                     min = max = i;                     isFirstTime = false;                 }                 else                 {                     if (i < min) min = i;                     if (i > max) max = i;                 }             }         }         System.out.println("Minimum = " + Integer.toString(min));         System.out.println("Maximum = " + Integer.toString(max));```
• 11-23-2010, 02:23 PM
couling
In short no. Initialising a variable means setting its value. There is no difference between the two.

Java will not let you read from a variable if it can not proove that its value has been set. Otherwise there would be a risk that it would simply read whatever the last program left in memory.

Java's ability to check that you've set a variable is quite unintelligent. But predicting when java will or wont accept a variable as being set is still a little tricky though. But avoid setting it inside a loop.

```int x = 0; // yes the =0 is needed! boolean xSet = false; for (int i=0; i<10; i++) {     int y = readValue();     if (!xSet) {         x = y;         xSet = true;     }     else if (x > y) {         x = y;     }     // .... }```