Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem.

Options
  • 20-08-2012 2:16pm
    #1
    Closed Accounts Posts: 3,249 ✭✭✭


    Trying to write a simple java program that finds the digital root of an integer. And I just couldnt get anything to work. So I scaled it back to the first step just to compute the sum of the individual numbers.

    Input - 123
    output - 1+2+3 = 6

    But even that wont work and I just cant figure it out. The console freezes which means its gone into an infinite loop or something right ?

    This is the code. Where am I going wrong ?

    /* DigitalRoot.java
     * -------------
     *  Finds the digital root of an integer.
     */
    
    import acm.program.*;
    
    public class DigitalRoot extends ConsoleProgram {
    
    	
    	public void run(){
    		println("Finds the digital root of any integer.");
    		int n = readInt("Enter integer: ");
    		int dsum = 0;
    		while (n > 0); {
    			dsum += n % 10;
    			n /= 10;
    		}
    		println(dsum);
    	}
    
    }
    


Comments

  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    You could read in the number as a string, convert it to a character array and iterate over the array:
    int i = 0;
    for (char c : input.toCharArray()) {
    	i += Character.getNumericValue(c);
    }
    


  • Closed Accounts Posts: 3,249 ✭✭✭Scioch


    jester77 wrote: »
    You could read in the number as a string, convert it to a character array and iterate over the array:
    int i = 0;
    for (char c : input.toCharArray()) {
    	i += Character.getNumericValue(c);
    }
    

    Little bit ahead of where I am and I dont want to get ahead of myself, this code should work though shouldnt it ? Do you see any reason why I wouldnt get an output cos I'm stumped.


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    Code looks ok, but you have a ; after the while loop that should not be there. If you take that out it should run ok, otherwise it will run forever as n is always > 0.


  • Closed Accounts Posts: 3,249 ✭✭✭Scioch


    jester77 wrote: »
    Code looks ok, but you have a ; after the while loop that should not be there. If you take that out it should run ok, otherwise it will run forever as n is always > 0.

    That was it !!

    I may look into the significance of that because I havent a clue why that makes a difference. :o


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    Scioch wrote: »
    That was it !!

    I may look into the significance of that because I havent a clue why that makes a difference. :o

    The ; ends the statement, so you have an empty while statement.
    while (n > 0);
    

    So the while loop checks if n > 0 and then there is nothing to do. It then keeps repeating that until n <= 0, but that can never happen here, n is never changed. So if will just keep running.


  • Advertisement
  • Closed Accounts Posts: 3,249 ✭✭✭Scioch


    Ah yes I see. All makes sense now :o


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    Scioch wrote: »
    Ah yes I see. All makes sense now :o

    :)

    That was a nasty little bug for a beginner, as everything looks ok and you wouldn't get any compile errors.

    It might be a little advanced, but it would be worth your while spending an hour or 2 looking into debugging as it would solve a lot of headaches further on. With the debugger you can step through your code line by line while it is executing and you would quickly see what lines are getting executed and you can monitor the value of your variables as the program runs.

    Nice article here, this guy writes easy to follow tutorials


  • Closed Accounts Posts: 3,249 ✭✭✭Scioch


    jester77 wrote: »
    :)

    That was a nasty little bug for a beginner, as everything looks ok and you wouldn't get any compile errors.

    It might be a little advanced, but it would be worth your while spending an hour or 2 looking into debugging as it would solve a lot of headaches further on. With the debugger you can step through your code line by line while it is executing and you would quickly see what lines are getting executed and you can monitor the value of your variables as the program runs.

    Nice article here, this guy writes easy to follow tutorials

    Thanks, I check that out. A bit of familiarity with debugging now would probably benefit me no end once I get into more complex programs. :)


Advertisement