What is the mistake in this code, I am able to print the total digits of the input number, but when I try to print the actual number from the scanner it always prints zero(0). For example the user enters 123456 as the input, and my program is correctly returning the count in this case it is 6. Here is my code

import java.util.Scanner;

public class CountNumbers {
    public static int count(int number) {
        int i = 0;
        while(number > 0 ){
            number = number / 10;
            i ++;
        System.out.println("The length of the "+ number +" is : " +i);
        return i;

    public static void main(String args[]){
        int number;
        System.out.print("Please enter the number to find the total digits");
        Scanner input = new Scanner(System.in);
            number = input.nextInt();
        catch(Exception e){



and here is the actual output from the console

Please enter the number to find the total digits123456
The length of the 0 is : 6

I don't get why it is always printing 0 instead of the actual number.

Varun Krishna. P

Your loop (line 6) keeps dividing number by 2 until it's zero - then you print it. So of course it's zero.
One fix is to use a copy of number in the loop, so the original number isn't changed.

This question has already been answered. Start a new discussion instead.