Thread: Method Problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Method Problem


    i'm trying to print between x and y Harshad number is an integer that is divisible by the sum of its digits. However; the colored part is unreachable code in eclipse.



    public void run () {
    int x = readInt("Please enter the first number :");
    int y = readInt("Please enter the second number: ");

    printHarshadNumbers(x,y);
    }

    public int getDigit(int i) {
    int result = 0;
    while (true) {
    result = i%10;
    i /= 10;

    }

    return result;
    }




    public void printHarshadNumbers(int x, int y) {


    for(int i=x;i<=y;i++) {
    if(i%getDigit(i)==0) {
    println(i+" is a Harshad number.");
    }

    }

    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,540
    Rep Power
    1906
    The while(true) loop in the getDigit(int) function never stops.

    Maybe you can explain a bit further what you want/expect?

    Please use code tags when posting code; this will keep the indention and make the code easier to read.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by MrFujin
    The while(true) loop in the getDigit(int) function never stops.

    Maybe you can explain a bit further what you want/expect?

    Please use code tags when posting code; this will keep the indention and make the code easier to read.
    Firstly, thanks for reply. I want to print Harshad number between integer x and y. A Harshad number is an integer that is divisible by the sum of its digits. My solution is taking digit sum from a method and using it in other method to control that is it harshad number or not.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,540
    Rep Power
    1906
    Originally Posted by intergalactic
    My solution is taking digit sum
    Where do you get the sum?

    What do you expect from the while(true) loop? That it should run forever as it does now?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by MrFujin
    Where do you get the sum?

    What do you expect from the while(true) loop? That it should run forever as it does now?
    i expect from the loop to get the sum of digits. I know there must be break condition to stop forever run, but i don't know how.


    result ++; is in while loop of course.
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,540
    Rep Power
    1906
    Originally Posted by intergalactic
    i expect from the loop to get the sum of digits. I know there must be break condition to stop forever run, but i don't know how.
    You can have something like this to end the loop:
    Code:
    if(condition)
      break;
    .

    But you have to define what the condition has to be to exit the loop.

    Originally Posted by intergalactic
    result ++; is in while loop of course.
    You should try to include all part of the code that is relevant to the question.
    Not sure why you use result ++;
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by MrFujin
    You can have something like this to end the loop:
    Code:
    if(condition)
      break;
    .

    But you have to define what the condition has to be to exit the loop.



    You should try to include all part of the code that is relevant to the question.
    Not sure why you use result ++;
    i made some changes.

    Code:
    if (i==0) break;
    and

    Code:
    result += i%10;
    result ++; was for storage.

    Finally, the code works, mucho gracias.

IMN logo majestic logo threadwatch logo seochat tools logo