This is the second solution to find the length of a given primitive int data type value. This approach converts int value to string, and then applies 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);
// Convert Integer to String, and then use the length() function
String int2Str=String.valueOf(numValue);
System.out.println("The Length of the give num "+numValue+" is="+int2Str.length());
}
}
Output:
Enter an Integer=
324
The integer is=324
The Length of an Integer 324 is=3

Leave a comment