Results 1 to 3 of 3
- 07-09-2008, 10:19 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 8
- Rep Power
- 0
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:
I made bold where it is telling me that remainder may have not been initialized.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 have been working on this for days now and it is due tomorrow :(
Any help would be really appreciated!
- 07-09-2008, 10:28 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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
toJava Code:int remainder; //The remainder of counter / locker.
orJava Code:int remainder = 0; //The remainder of counter / locker.
The second if you want a value that should never occur inside the while loop.Java Code:int remainder = -1; //The remainder of counter / locker.
- 07-09-2008, 10:42 PM #3
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
-
How to initialize an Array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:47 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:22 AM -
How to initialize array at runtime
By Java Tip in forum Java TipReplies: 0Last Post: 11-09-2007, 03:47 PM -
Initialize array at runtime
By javaplus in forum Java TipReplies: 2Last Post: 11-09-2007, 11:44 AM -
I do not know how to initialize the two variables
By Daniel in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks