Results 1 to 5 of 5
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default Re: Infinate Loop? (Crashing program)

    Where are you changing decimalValue in your loop?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Infinate Loop? (Crashing program)

    Quote Originally Posted by jim829 View Post
    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. #4
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2013, 10:42 AM
  2. Need help with program using For loop
    By .Tyler in forum New To Java
    Replies: 0
    Last Post: 10-20-2011, 09:02 PM
  3. loop program
    By javanovoice in forum New To Java
    Replies: 12
    Last Post: 09-09-2011, 04:01 AM
  4. Loop Program Help Please?
    By rjones215 in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 11:50 PM
  5. While loop crashing?
    By ToplessGrunt in forum New To Java
    Replies: 6
    Last Post: 10-20-2008, 12:07 PM

Posting Permissions

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