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);
try{
number = input.nextInt();
count(number);
}
catch(Exception e){
e.printStackTrace();
}
finally{
input.close();
}
}
}
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.
Thanks
Varun Krishna. P