Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Helper {
- //validar numeros positi
- public static int validarPositivo(Scanner opcion , String mensaje){
- int numero;
- String linea;
- while (true) {
- try {
- System.out.println(mensaje);
- linea = opcion.nextLine();
- numero = Integer.parseInt(linea);
- while(!(numero>=0)){
- System.out.println("''ERROR:Ingrese un numero positivo!''");
- System.out.println(mensaje);
- linea = opcion.nextLine();
- numero = Integer.parseInt(linea);
- }
- break;
- } catch (Exception e) {
- System.out.println("''ERROR..! Ingrese un numero''");
- }
- }
- return numero;
- }
- //validar opcion random o manual
- public static int elegirOpcion(Scanner opcion , String mensaje){
- int numero;
- String linea;
- while (true) {
- try {
- System.out.println(mensaje);
- linea = opcion.nextLine();
- numero = Integer.parseInt(linea);
- while(!(numero<=4 && numero>=1)){
- System.out.println("Opcion Incorrecta");
- System.out.println(mensaje);
- linea = opcion.nextLine();
- numero = Integer.parseInt(linea);
- }
- break;
- } catch (Exception e) {
- System.out.println("''ERROR..! Ingrese un numero''");
- }
- }
- return numero;
- }
- //validar que se ingrese solamente letras primera parte
- public static String cargarLetra(Scanner entrada,String mensaje){
- System.out.println(mensaje);
- String linea=entrada.nextLine();
- while(validarLetras(linea)){
- System.out.println("''ERROR'' No Ingresar Numeros");
- System.out.println(mensaje);
- linea=entrada.nextLine();
- }
- return linea;
- }
- //validar que se ingrese solamente letras segunda parte
- public static boolean validarLetras(String linea) {
- for (int x = 0; x < linea.length(); x++) {
- char c = linea.charAt(x);
- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ' ')) {
- return true;
- }
- }
- return false;
- }
- //Genera caracter random
- public static String caracterRandom() {
- Random random = new Random();
- char caracterRandom = (char)(random.nextInt(26) + 'a');
- System.out.println("Caracter generado: "+ caracterRandom);
- String caracter = String.valueOf(caracterRandom);
- return caracter;
- }
- //Genera random con parametro a peticion
- public static int generarRandom(String mensaje,int x) {
- System.out.println(mensaje);
- int numRandom = (int) (Math.random() * x);
- System.out.println("Valor generado: "+numRandom );
- return numRandom;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment