CHECK VALIDATE INPUT

 avatar
unknown
plain_text
a year ago
3.4 kB
9
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package linearsearch;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class checkValidateInput {

    private Scanner scanner;

    public checkValidateInput() {
        scanner = new Scanner(System.in);
    }

    public int getPositiveIntegerInput() {
        int Input;
        while (true) {  // Start an infinite loop to keep asking for input until a valid value is provided.
            try {
                System.out.println("Input Number of Array : ");
                Input = Integer.parseInt(scanner.nextLine());  // Read the user's input and parse it as an integer.
                if (Input > 0) {
                    break;
                    // if size > 0 , valid value and loop break 
                } else {
                    System.err.println("You have to input a positive number , Reinpiut : ");
                    // iff size <= 0 , Display notice 
                }
            } catch (NumberFormatException e) {
                System.err.println("Invalid value , Reinput : ");
                // if users input a special character , program will notice to users 
            }
        }
        return Input;
    }

    public String getStringInput() {
        String str;
        while (true) {
            System.out.println("Input string : ");
            str = scanner.nextLine();
            if (!str.trim().isEmpty()) {
                break;
            } else {
                System.err.println(" String must not empty !");
            }
        }
        return str;
    }

    public double getDoubleInput() {
        double Input;
        while (true) {  // Start an infinite loop to keep asking for input until a valid value is provided.
            try {
                System.err.println("Input Number of Array : ");
                Input = Double.parseDouble(scanner.nextLine());  // Read the user's input and parse it as an integer.
                if (Input > 0) {
                    break;
                    // if size > 0 , valid value and loop break 
                } else {
                    System.err.println("You have to input a positive number , Reinpiut : ");
                    // iff size <= 0 , Display notice 
                }
            } catch (NumberFormatException e) {
                System.err.println("Invalid value , Reinput : ");
                // if users input a special character , program will notice to users 
            }
        }
        return Input;
    }

    public char getCharacterInput() {
        char character;
        while (true) {
            System.out.println("Input a character: ");
            String input = scanner.nextLine();
            if (input.trim().isEmpty()) {
                System.err.println("No character are entered ");
                continue;
            } else if (input.length() == 1) {
                character = input.charAt(0);
                break;
            } else {
                System.err.println("Input only 1 character ");
            }
        }
        return character;
    }

    public void closeScanner() {
        scanner.close();
    }
}
Editor is loading...