This is the third solution to find the length of a given primitive int data type value. This approach uses Integer wrapper class to convert to string before using the length method.
import java.util.Scanner;
public class LengthOfPrimitiveInt {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpInt= new Scanner(System.in);
//Input an Integer
System.out.println("Enter an Integer=");
int numValue=inpInt.nextInt();
System.out.println("The integer is="+numValue);
// Use the Wrapper class of an int i.e. Integer to find out the length
System.out.println("The Length of the given num "+numValue+" is="+Integer.toString(numValue).length());
}
}
Output:
Enter an Integer=
324
The integer is=324
The Length of an Integer 324 is=3

Leave a comment