Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Helper {
- static Scanner entrada = new Scanner(System.in);
- public static int forcePositiveIntEnter(String msg) {
- int size = 0;
- while(true){
- System.out.println(msg);
- try{
- size = Integer.parseInt(entrada.nextLine());
- if (size > 0) break;
- else System.out.println("Validos numeros mayores que 0");
- }
- catch(NumberFormatException e){
- System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS POSITIVOS. "+e);
- }
- }
- return size;
- }
- public static int forceIntegerWithoutZ(String msg){
- int n = 0;
- while(true){
- System.out.println(msg);
- try{
- n = Integer.parseInt(entrada.nextLine());
- if (n != 0) break;
- else System.out.println("Ingrese un numero mayor a 0");
- }
- catch(NumberFormatException e){
- System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
- }
- }
- return n;
- }
- public static int forceInteger(String msg){
- int n = 0;
- while(true){
- System.out.println(msg);
- try{
- n = Integer.parseInt(entrada.nextLine());
- break;
- }
- catch(NumberFormatException e){
- System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
- }
- }
- return n;
- }
- public static int forcePositiveIntegerWithZ(String msg){
- int n = 0;
- while(true){
- System.out.println(msg);
- try{
- n = Integer.parseInt(entrada.nextLine());
- if (n >=0) break;
- else System.out.println("Ingrese un numero mayor o igual a 0");
- }
- catch(NumberFormatException e){
- System.out.println("SOLAMENTE VALIDOS NUMEROS ENTEROS. "+e);
- }
- }
- return n;
- }
- public static void showFinalResults(String msg) {
- System.out.println("************FIN DE EJECUCION************");
- System.out.println("Se mostraran resultados finales: ");
- System.out.println(msg);
- System.out.println("******************FIN******************");
- }
- public static int menuManualRandom() {
- int option = 0;
- while (true){
- try{
- //mostrar e ingresar valores para el menu
- System.out.println("---------------MENU---------------");
- System.out.println("1- Ingresar valores manuales");
- System.out.println("2- Ingresar valores aleatorios");
- option = Integer.parseInt(entrada.nextLine());
- //corroborar si es correcta la opcion elegida
- if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
- else return option;
- }
- catch (NumberFormatException e){
- System.out.println("Ingrese valor NUMERICO valido" + e);
- }
- }
- }
- public static int randomIntGenerator(int min, int max) {
- int x = (int) ((int)(Math.random()*((max-min)+1))+min);
- return x;
- }
- public static boolean continueProgram(){
- while (true){
- System.out.println("Desea continuar con la ejecucion del programa? Opciones validas: (S/s || N/n)");
- String resp = entrada.nextLine();
- if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
- else System.out.println("Opcion no valida");
- }
- }
- public static int menuProductoSuma() {
- int option = 0;
- while (true){
- try{
- //mostrar e ingresar valores para el menu
- System.out.println("---------------MENU---------------");
- System.out.println("1- Producto de una Fila");
- System.out.println("2- Suma de una Columna");
- option = Integer.parseInt(entrada.nextLine());
- //corroborar si es correcta la opcion elegida
- if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
- else return option;
- }
- catch (NumberFormatException e){
- System.out.println("Ingrese valor NUMERICO valido" + e);
- }
- }
- }
- public static int menuOriginalTranspuesta() {
- int option = 0;
- while (true){
- try{
- //mostrar e ingresar valores para el menu
- System.out.println("---------------MENU---------------");
- System.out.println("1- Matriz Original");
- System.out.println("2- Matriz Transpuesta");
- option = Integer.parseInt(entrada.nextLine());
- //corroborar si es correcta la opcion elegida
- if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
- else return option;
- }
- catch (NumberFormatException e){
- System.out.println("Ingrese valor NUMERICO valido" + e);
- }
- }
- }
- public static boolean continueCargaFibonacci(){
- while (true){
- System.out.println("Desea seguir Cargando datos de Fibonacci? (S/s=si || N/n=no)");
- String resp = entrada.nextLine();
- if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
- else System.out.println("Opcion no valida");
- }
- }
- public static boolean continueCargaLibro(){
- while (true){
- System.out.println("Desea seguir Cargando Libros? (S/s=si || N/n=no)");
- String resp = entrada.nextLine();
- if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
- else System.out.println("Opcion no valida");
- }
- }
- public static Float getFloat(Scanner entrada, String inputMessage, String errorMessage) {
- Float floatValuer = 0f;
- while (true) {
- try {
- System.out.print(inputMessage);
- floatValuer = Float.parseFloat(entrada.nextLine());
- // return BigDecimal.valueOf(d).setScale(decimalPlace,BigDecimal.ROUND_HALF_UP).floatValue();
- return ((int) ((floatValuer + 0.005f) * 100)) / 100f;
- }
- catch (Exception exception) {
- System.out.println(errorMessage);
- }
- }
- }
- public static Float getFloat(Scanner entrada, String inputMessage) {
- return getFloat(entrada, inputMessage, "Ingrese un número válido\n");
- }
- public static Float getFloat(String inputMessage, String errorMessage) {
- return getFloat(Helper.entrada, inputMessage, errorMessage);
- }
- public static Float getFloat(String inputMessage) {
- return getFloat(Helper.entrada, inputMessage, "Ingrese un número válido\n");
- }
- public static int menuAutorEditorial() {
- int option = 0;
- while (true){
- try{
- //mostrar e ingresar valores para el menu
- System.out.println("---------------MENU---------------");
- System.out.println("1- Buscar Libro por Autor");
- System.out.println("2- Buscar Libro por Editorial");
- option = Integer.parseInt(entrada.nextLine());
- //corroborar si es correcta la opcion elegida
- if (option != 1 && option != 2) System.out.println("Valor no contemplado en las opciones del menu");
- else return option;
- }
- catch (NumberFormatException e){
- System.out.println("Ingrese valor NUMERICO valido" + e);
- }
- }
- }
- public static String getValidsString(String msg){
- while(true){
- try{
- System.out.println(msg);
- String rtnString = entrada.nextLine();
- if (rtnString.isEmpty() || rtnString.charAt(0) == ' ') Integer.parseInt("s");
- return rtnString;
- }
- catch(Exception e){
- System.out.println("Ingrese una cadena valida. Sin espacios al comienzo.");
- }
- }
- }
- public static boolean continueCargaAutores(){
- while (true){
- System.out.println("Desea seguir Cargando Autores? (S/s=si || N/n=no)");
- String resp = entrada.nextLine();
- if ("s".equals(resp.toLowerCase()) || "n".equals(resp.toLowerCase())) return ("s".equals(resp.toLowerCase()));
- else System.out.println("Opcion no valida");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment