Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Helper {
- public static Scanner scanner = new Scanner(System.in);
- public static Random random = new Random();
- public static Integer getPositiveInt(String message) {
- while(true){
- try {
- System.out.println(message);
- int num = Integer.parseInt(scanner.nextLine());
- if(num > 0) return num;
- System.err.println("\nError: no ingresó un número entero positivo");
- } catch (NumberFormatException e) {
- System.err.println("\nError: ingresó un caracter inválido");
- }
- }
- }
- private static Integer getInt(String inputMessage, String errorMessage) {
- while(true){
- try {
- System.out.println(inputMessage);
- return Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException e) {
- System.err.println(errorMessage);
- }
- }
- }
- public static Integer getInt(String inputMessage) {
- return getInt(inputMessage, "\nError: el valor ingresado no es un número entero");
- }
- public static char yesOrNo(String question){
- char resp;
- do {
- System.out.println("\n" + question + "\nPresione 'S' (SI) o 'N' (NO)");
- resp = Character.toUpperCase(scanner.nextLine().charAt(0));
- if(resp == 'N' || resp == 'S')return resp;
- System.err.println("\nError: lo ingresado no es una opción correcta");
- } while (true);
- }
- public static int randomInt(int min,int max) {
- return random.nextInt(max - min + 1) + min;
- }
- public static void pressEnterKeyToContinue()
- {
- System.out.println("\nPulse una tecla para continuar...");
- scanner.nextLine();
- }
- public static String getString(String message) {
- System.out.println(message);
- return scanner.nextLine();
- }
- public static boolean isValidadWord(String nombre) {
- return nombre.matches("[A-Za-zÁÉÍÓÚáéíóúñÑ]+");
- }
- public static String getValidWord(String inputMessage, String errorMessage) {
- String palabra;
- while(true) {
- System.out.println(inputMessage);
- palabra= scanner.nextLine();
- if (isValidadWord(palabra)) {
- return palabra;
- } else {
- System.err.println(errorMessage);
- }
- }
- }
- public static String getValidWord(String inputMessage) {
- return getValidWord(inputMessage, "\nERROR: Solo se admiten letras, sin espacios");
- }
- }
Add Comment
Please, Sign In to add comment