Mo0nbase
Mo0nbase

Reputation: 123

Why is code skipping over for loop in integer length program

Im trying to create a program to find the length of a given number. I thought i would do this by taking the number and dividing by 10 and then checking to see if the number was <= 0. I dident want to edit the global number so i created a instance version of the number and used that as the condition in the for loop.

So obviously this dident work so naturally i ended up looking in the debugger to figure out what was going on. It looks as if the program is completely skipping over the for loop any help would be appreciated.

public static void sumFirstAndLastDigit(int number) {
        int numberLength = 0;
        int instanceNumber = number;
        for(int i = 0; instanceNumber <= 0; i++) {
            instanceNumber /= 10;
            numberLength = i;   
        }
        System.out.println("Number length = " + numberLength);
        // to find length of number loop division by 10
    }
}

The program should use the for loop to keep dividing by 10 until the number is = to or less than than zero and for how many times the loop ran should be stored in the number length integer. In this case with the number 12321 the answer should be 6 but it prints 0.

Upvotes: 1

Views: 79

Answers (4)

T.J. Crowder
T.J. Crowder

Reputation: 1075765

You're telling it to loop while instanceNumber <= 0. The "test" in a for loop is a "keep going" test, not a termination test. The loop continues as long as the test is true.

From your description, you want instanceNumber > 0.

Also note Avinash Gupta's point that with your current code, you'll undercount by one. I'd address that by using a completely different loop:

int numberLength = 0;
int instanceNumber = number;
while (instanceNumber > 0) {
    ++numberLength;
    instanceNumber /= 10;
}

That's nice and unambiguous: If instanceNumber > 0, it increments numberLength, then divides by 10 and tries again.

Upvotes: 5

gokareless
gokareless

Reputation: 1253

Consider even more sophisticated solution:

public static void sumFirstAndLastDigit(int number) {
    int numberLength = (int) (Math.log10(number) + 1);
    System.out.println("Number length = " + numberLength);
}

Taken from Baeldung

Upvotes: 0

Avinash Gupta
Avinash Gupta

Reputation: 228

This will print the correct output

public static void sumFirstAndLastDigit(int number) {
    int numberLength = 0;
    int instanceNumber = number;
    for(int i = 0; instanceNumber > 0; i++) {
         instanceNumber /= 10;
         numberLength = i;   
     }
     System.out.println("Number length = " + (numberLength + 1));

    }

Upvotes: 2

Yevgen
Yevgen

Reputation: 1667

Your code will be much more comprehensive if you use while loop for your algorithm.

public static void sumFirstAndLastDigit(int number) {
    int numberLength = 0;
    int instanceNumber = number;
    while(instanceNumber != 0) {
        instanceNumber /= 10;
        numberLength += 1;
    }
    System.out.println("Number length = " + numberLength);
    // to find length of number loop division by 10
}

Upvotes: 0

Related Questions