# Infinate Loop? (Crashing program)

• 10-02-2013, 07:45 PM
kkid
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.

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.
• 10-02-2013, 08:02 PM
jim829
Re: Infinate Loop? (Crashing program)
Where are you changing decimalValue in your loop?

Regards,
Jim
• 10-02-2013, 08:29 PM
kkid
Re: Infinate Loop? (Crashing program)
Quote:

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.
• 10-02-2013, 08:49 PM
kkid
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?
• 10-02-2013, 10:22 PM
jim829
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