Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default Int does not initialize, will this work?

    I am a new java student, recently started taking a class on java.

    I have two questions regarding this program that I have to write.

    First, I am using eclipse and an error is occurring in my if () statement. It is saying that my int, remainder, may not have initialized. I do not understand why it wouldn't be initialized. Any ideas??

    Also, because it isn't initialized I can not run the program to find out if everything else is working the way it should.

    To explain, what I have to do is:
    1. have a user input a number, called locker.
    2. Find out if the remainder of counter (starting at 1 until <= locker) equals 0.
    3. If it doesn't equal 0 than I want my int odds to count how many times the counter does not equal 0.
    4. output the number of times to the user.
    5. It is required that I use JOptionPane and I want to allow the user to enter a different number without having to restart the program.

    Here is my program:

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*;
    
    public class CH05EX16 {
    
    	public static void main(String[] args) 
    	{
    		int locker; //The locker number, user input.
    		int counter = 1; //The student, loop control variable 
    		int odds = 0; //The number of open lockers.
    		int remainder; //The remainder of counter / locker. 
    		String inputMessage;
    		String outputMessage;
    		String inputStr;
    		
    		inputMessage = "Enter the number of lockers "
    					 + "in your school.";
    					 
    		inputStr = JOptionPane.showInputDialog(inputMessage);
    		locker = Integer.parseInt(inputStr);
    		
    		while (counter <= locker)
    		{	
    			remainder = (counter % locker);
    			counter++;
    		}
    		if ([B]remainder[/B] != 0)
    			{
    			odds++;
    			outputMessage = "The number of lockers that are open are: " + odds;
    			}
            inputMessage = "Enter a different number of lockers: ";	
            
            inputStr = JOptionPane.showInputDialog(inputMessage);
    		locker = Integer.parseInt(inputStr);
    		
    		System.exit(0);
    	}
    }
    I made bold where it is telling me that remainder may have not been initialized.

    I have been working on this for days now and it is due tomorrow :(

    Any help would be really appreciated!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, you declare remainder inside your method and don't immediately define it, so it has not been initialised.

    Then, the only time you assign a value to it, is inside of a while loop. The compiler sees this, and immediately recognises that if the while loop is never entered (say when counter starts out larger than locker), then remainder will never have been initialised.

    The compiler doesn't care what you have initially defined counter and locker as (it doesn't consider their values), it only notices that remainder is only ever defined inside a block of code that might never execute.

    Change
    Java Code:
    int remainder; //The remainder of counter / locker.
    to
    Java Code:
    int remainder = 0; //The remainder of counter / locker.
    or
    Java Code:
    int remainder = -1; //The remainder of counter / locker.
    The second if you want a value that should never occur inside the while loop.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default some help

    To make 'not initialized ...' go away, lift variable declaration to an exterior scope and assign it some value. Historically, before we had tools like C and Java, it was expected that variables would NOT be initalized. Walking through variables to set them to a reasonable value before use is only useful when there is a precondition to the method call that depends on prior instructions having been conducted reasonably.

    That is not obvious why, and is somewhat subtle - but extremely simple in nature.

    Many who have been instructed in contemporary practice will disagree with me, but if one cannot code such as to either initialize before dropping into a loop or check for existing value or make loop logic such that an assignment ( write ) occurs before a retrieve ( read ) then they never would have made it doing a backplane on a Type 82

    1. have a user input a number, called locker.
    Console (Java Platform SE 6)

    2. Find out if the remainder of counter (starting at 1 until <= locker) equals 0.

    if(--counter > 0); or if( lockerNumber > totalLockers);
    Several approaches here.

    3. If it doesn't equal 0 than I want my int odds to count how many times the counter does not equal 0.

    if(counter != 0)++odds;//

    4. output the number of times to the user.
    System.out.println( Integer.toString( NumberOfTimes));//

    5. It is required that I use JOptionPane and I want to allow the user to enter a different number without having to restart the program.

    int result =o;
    while((result = JOptionPane.getResul;t()) != 0){ code; code; code; code;}
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:47 PM
  2. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:22 PM
  3. How to initialize array at runtime
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 04:47 PM
  4. Initialize array at runtime
    By javaplus in forum Java Tip
    Replies: 2
    Last Post: 11-09-2007, 12:44 PM
  5. I do not know how to initialize the two variables
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:42 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
  •