In: Computer Science
• 2. Get all the information from the user using methods Java •
B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
I have implemented a java program which contains below two methods::
1> public void getName(Scanner sc) :- This method get the name from the user using Scanner class object and also check whether name contains string or number. If name contains digit/number then it will prompt to the user please enter proper input again method is called itself for getting the user input.
2> public void getAge(Scanner sc) :- This method get the age from the user using Scanner class object and also check whether agecontains string or number. If age contains string/character then it will prompt to the user please enter proper input again method is called itself for getting the user input.
Program:-
import java.util.Scanner;
public class TestUser {
    
    // store name in string data type
    String name;
    
    // store age in int data type
    int age;
    
    
    /**
     * This method get the name from the user
     * @param sc Scanner object which is used to get input from the user
     */
    public void getName(Scanner sc){
        
        // get the name from the user
        System.out.print("Enter name:");
        String name = sc.nextLine();
        
        boolean invalidName = false;
        
        // now check whether srtring contain digit
        for(int i=0; i<name.length(); i++){
            
            //  check the digit using isDigit() method 
            if(Character.isDigit(name.charAt(i))){
                invalidName = true;
                break;
            }
        }
        
        // if the string name contains digit then
        if(invalidName){
            
            // display the message to the user
            System.out.println("Name can not be numbers");
            
            // call the method untill the user does not enter proper input
            getName(sc);
        }else{
            
            // Otherwise store to the instance data member of class
            this.name = name;
        }
        
    }
    
    /**
     * This method get the age from the user
     * @param sc Scanner object which is used to get input from the user
     */
    
    public void getAge(Scanner sc){
         // get the name from the user
        System.out.print("Enter age:");
        
        // first get the input from the user as a string
        String age = sc.nextLine();
        
        boolean invalidAge = false;
        
        // now check whether age contain character
        for(int i=0; i<age.length(); i++){
            
            //  check the digit using isDigit() method 
            if(!Character.isDigit(age.charAt(i))){
                invalidAge = true;
                break;
            }
        }
        
        // if the string name contains digit then
        if(invalidAge){
            
            // display the message to the user
            System.out.println("Age can not be String");
            
            // call the method untill the user does not enter proper input
            getAge(sc);
        }else{
            
            // Otherwise store to the instance data member of class by converting string to int
            this.age = Integer.parseInt(age);
        }
    }
    
    
    public static void main(String[] args) {
        
        // create an object of Scanner class
        Scanner sc = new Scanner(System.in);
        
        // create an object of TestUser class for calling methods
        TestUser user = new TestUser();
        
        // get the user name by calling getName() and passing scanner object
        user.getName(sc);
        
        // get the user age by calling getAge() and passing scanner object
        user.getAge(sc);
        
    }
}
Output:-

According to the output, if the user enter in proper input then program will prompt the user again to saying that please enter proper input. This process is running untill the user doen not enter the proper input.
I hope you will understand the above program.
Do you feel needful and useful then please upvote me.
Thank you.