Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Helper {
- public static Scanner scanner = new Scanner(System.in);
- public static int forceReadPositiveInteger(String msg)
- {
- int ret = 0;
- while(true)
- {
- try
- {
- System.out.println(msg);
- while((ret = Integer.parseInt(scanner.next())) < 1) System.out.println("El número a cargar debe ser positivo.\n"+msg);
- return ret;
- }catch (Exception e) {
- System.out.println("Error en la carga.");
- }
- }
- }
- public static char forceReadAnLetter(String msg)
- {
- char ret = '\0';
- while(true)
- {
- try
- {
- String aux;
- System.out.println(msg);
- while((aux = scanner.next()).length() > 1 || !Character.isAlphabetic((ret = aux.charAt(0))) ) System.out.println("Se debe cargar una sola letra.\n" + msg);
- return ret;
- }catch (Exception e) {
- System.out.println("Error en la carga.");
- }
- }
- }
- public static int getRandomIntBetweenRange(double min, double max){
- int x = (int) ((int)(Math.random()*((max-min)+1))+min);
- return x;
- }
- public static char generateRandomLetter()
- {
- return ((getRandomIntBetweenRange(0,17)% 2 == 0) ? (char) (getRandomIntBetweenRange(0, 25)+97): (char) (getRandomIntBetweenRange(0, 25)+65));
- }
- public static boolean isVowel(char xCh)
- {
- xCh = Character.toLowerCase(xCh);
- return xCh == 'a' || xCh == 'e' || xCh == 'i' || xCh == 'o' || xCh == 'u';
- }
- public static boolean isRandom()
- {
- char x = '\0';
- while(true)
- {
- try
- {
- System.out.println("¿Generar valores aleatorios? (S/N)");
- x =Character.toUpperCase(scanner.next().charAt(0));
- if(x == 'S' || x == 'N') return x == 'S';
- System.out.println("Opción inválida, reintente.");
- }catch (Exception e) {
- System.out.println("Opción inválida, reintente.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement