Results 1 to 4 of 4
- 11-23-2010, 01:39 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
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:
Java 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 #2
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
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:
Java 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 #3
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
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.
Looking briefly at your code:
If you want to avoid initialising the variables to 0 (allowing negative numbers as input) then you should use a boolean variable as an indicator.
eg:
Java Code: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; } // .... }
- 11-23-2010, 03:19 PM #4
Another common practice is to initialize your minimum variable to be Integer.MAX_VALUE and your maximum variable to be Integer.MIN_VALUE. That way you know they'll be set the first time around.
Or you could do the "first time" initialization. You don't even need any pesky booleans. Just do a scan before the loop to do the setting and loop one less time.
Similar Threads
-
Help with iniitalize int variable without assigning a number?
By equal in forum New To JavaReplies: 0Last Post: 11-23-2010, 01:32 PM -
Initialize a Variable
By lala in forum New To JavaReplies: 13Last Post: 11-16-2010, 07:51 PM -
how to initialize base class variable without creating object for that?
By kaka in forum New To JavaReplies: 3Last Post: 09-29-2010, 10:26 AM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 08:01 PM -
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06-10-2008, 04:53 AM
Bookmarks