# Thread: Infinate Loop? (Crashing program)

1. Senior Member
Join Date
Jul 2012
Posts
245
Rep Power
8

## Infinate Loop? (Crashing program)

I am making a program that takes a decimal integar and converts it into binary using the division method.

This is the action event to a submit button which I hoped would do this, however, when I click the submit button the program crashes. Have I somehow caused an infinate loop? I cannot see how I could have done.

Java Code:
```int decimalValue;
int modulusTemp;
int decimalTemp;
decimalValue = Integer.parseInt(decimalField.getText());
decimalTemp = decimalValue;
while (decimalValue > 0){
modulusTemp = decimalValue % 2;
if (modulusTemp == 0){
binaryField.setText(binaryField.getText() + "0");
decimalTemp = decimalTemp / 2;
}
else if (modulusTemp != 0){
binaryField.setText(binaryField.getText() + "1");
decimalTemp = (decimalTemp / 2) - 1;
}

}```
Note: decimalValue is the decimal input, modulusTemo is the value of the modulus for the last operation and decimalTemp is the value of the decimal as the divisions are taking place. decimalValue is not used in the calculation as I want to maintain the original value of input in a variable.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Infinate Loop? (Crashing program)

Where are you changing decimalValue in your loop?

Regards,
Jim

3. Senior Member
Join Date
Jul 2012
Posts
245
Rep Power
8

## Re: Infinate Loop? (Crashing program)

Originally Posted by jim829
Where are you changing decimalValue in your loop?

Regards,
Jim
oh dear. So obvious. It's meant to be decimalTemp in the while condition. I'll test it out now. Thanks

My algorythm is a little messed up, giving me incorrect answers. But I can sort that out with a little more thought. It doesn't crash now though. Thanks.

4. Senior Member
Join Date
Jul 2012
Posts
245
Rep Power
8

## Re: Infinate Loop? (Crashing program)

Just because this is a little too small to make a whole new thread for, I'll quickly ask here.

With integar division in Java, if there is anything other than perfect division (whole numbers as an answer), does Java round up?
So even if the number is 2.0000001, the answer given will be 3?
Am I remembering correctly?

Therefore, if when I do my modulus operation and I get an answer of 1 and then do the same again but with a division operation, the division will always be out my 1, right?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Infinate Loop? (Crashing program)

It depends on what you assign it too. If you divide a double by an int and assign to the double no special treatment is done. If you assign to an int, the fraction goes away.

Regards,
Jim

#### Posting Permissions

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