how can i limit the user to a certain input

38 views Asked by At

Hi I'm trying to learn java

This a simple program that stores a patient's blood details, well in any case how can i keep the program accurate by means of returning an error message if the input was not in the list of possible inputs.

protected static final String[] bloodType ={"A","O","B","AB"};
static final String[] rhFactor={"+","-"};

Well the topic was overloading constructors

class BloodData {
    
    protected static final String[] bloodType ={"A","O","B","AB"};
    static final String[] rhFactor={"+","-"};
    
    private String setBloodType;
    private String setRhFactor;
    
    public BloodData(){
        
        this.setBloodType = "O";
        this.setRhFactor = "+";
        
    }
    public BloodData(String bt, String rh){
        this.setBloodType=bt;
        this.setRhFactor=rh;
    }
    public void display(){
       
        System.out.println(setBloodType+setRhFactor + " is added to the blood bank.");
    }
}
public class RunBloodData {

    public static void main(String[] args) {
        
        try {
            do {   
//            String[] input1={"A","B","O","AB"};    how can i limit the user to a certain input
//            String[] input2={"+","-"};
            Scanner scan= new Scanner(System.in);
        System.out.print("Enter the Blood Type of patient: ");
        String input1 =scan.nextLine();
        
        System.out.print("Enter the Rhesus Factor(+ or -): ");
        String input2 =scan.nextLine();
        
            if (input1.isBlank()||input2.isBlank()) {
                BloodData bd = new BloodData();
                bd.display();
            } else {
                BloodData bd = new BloodData(input1, input2);
                bd.display();
            }
        
        
        } while (true);
        } catch (InputMismatchException e) {
            System.out.println("Error in your input!");
        }
        
        
    }
    
    
}

Output:

Enter the Blood Type of patient: 
Enter the Rhesus Factor(+ or -): 
O+ is added to the blood bank.
Enter the Blood Type of patient: 
0

There are 0 answers