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
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
JAVA) I need to get 10 integer numbers from the user. Then I need to find...
JAVA) I need to get 10 integer numbers from the user. Then I need to find sum of odd numbers, sum of even numbers, the lowest number of all numbers, the highest number of all numbers, and the average of all numbers( use double, with the two digit decimal) process; loopCount = 1 While LoopCount <= 10 Read number from the keyboard If odd, add to the total of all odd numbers If even, add to the total of all...
**** 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...
Assume all methods in the Stack class are available to you, request from the user a...
Assume all methods in the Stack class are available to you, request from the user a set of numbers (terminated by -1) and print them in reverse order . write code in java.
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.
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...
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
• 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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT