Question

In: Computer Science

• 2. Get all the information from the user using methods Java • B. if the...

• 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)

Solutions

Expert Solution

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.


Related Solutions

2. Create a php program to get all the values from the forms using various methods...
2. Create a php program to get all the values from the forms using various methods and control structures like switch, if else, for, foreach, while, do while The question is required to write a program
Can you write code or class in java accepting from the user a + b as...
Can you write code or class in java accepting from the user a + b as a string and accepting their value from the user and the out but will be the sum of the variables.
**** All these methods should be implemented using RECURSIVE solutions (no looping statements) // Java //...
**** All these methods should be implemented using RECURSIVE solutions (no looping statements) // Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a...
Write a method in JAVA to do the following: As the user to select from 2...
Write a method in JAVA to do the following: As the user to select from 2 different (default) pokemon or load their pokemon by giving you the name of their pokemon. Based on their selection (default or supplied name), read the move list and damage range from the input file(you do not need to create this) for the selected pokemon. Randomly select one of the default pokemon - Bulbasaur, Charmander, Squirtle (or you can add additional computer only options if...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. d) Perform the functionalities(Deposit, Withdraw and print) until the user selects to stop.
• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. b) Show different functionalities of a bank account (Deposit, Withdrawal, Print)
• 1. Write a java program using methods to find the information about a account holder...
• 1. Write a java program using methods to find the information about a account holder at bank. c) For every transaction make sure make use of his account balance.
User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:
• User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:o usernameo firstNameo lastNameo a list of the 10 most recently purchased itemso A user can bid on and purchase ItemsThe User class should have a default constructor, as well as one accepting all parameters. It should also provide accessor (getter) and mutator (setter) methods for appropriate methods. By default, a user is able to buy products only (not...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT