Normal method:
import java.util.Scanner;public class Digits { public static void main(String[] args){ Scanner input=new Scanner(System.in);//Declare the scanner variable System.out.println("Please enter 0-999999999 integer");//The system prompts to enter try{ //Speak out exception while(true){ int num=input.nextInt(); int count = 0; if (num < 0 || num > 999999999) System.out.println("Input out of range"); else if (num==0) System.out.println("Input is a 1-digit number"); else { while(num > 0){ num=num / 10; count++; } System.out.println("Input is a '+count+' digit number"); } } } catch (Exception e){ //Catch the exception System.out.println("Please enter correctly"); e.printStackTrace(); //Print the location and reason of the error in the program} } } }General functions/methods:
import java.util.Scanner;public class Digits { boolean digits(int num){ //Create method of boolean type if (num < 0 || num > 9999999999){ return true; } else{ return false; }} public static void main(String[] args){ Digits d=new Digits (); //Create object Scanner input=new Scanner(System.in); //Declare the scanner variable System.out.println("Please enter 0-99999999999 integer"); //The system prompts to enter try{ // Listen to the exception while(true){ int num=input.nextInt();//Get the value input in the next line int count=0; if(num==0){ System.out.println("Input is a 1-digit number"); } else if(d.digits(num)){ //The object calls the digits method System.out.println("Input is out of range"); } else{ while(num > 0){ num=num / 10; count++; } System.out.println("Input is a 1-digit number"); } } } catch (Exception e){ //Catch the exception System.out.println("Please enter correctly"); e.printStackTrace(); //Print the location and reason of the error in the program} } } }Note: Method 2 uses object-oriented thinking