Results 1 to 4 of 4
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default 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);
    	
    			
    			
    	
    	}
    			
    		
    	
    	
    	}
    	
    
    }

  2. #2
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    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));

  3. #3
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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;
        }
        // ....
    }
    Hope this helps

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    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

  1. Replies: 0
    Last Post: 11-23-2010, 12:32 PM
  2. Initialize a Variable
    By lala in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 06:51 PM
  3. Replies: 3
    Last Post: 09-29-2010, 09:26 AM
  4. Replies: 3
    Last Post: 11-24-2008, 07:01 PM
  5. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •