Advertisement
LEANDRONIEVA

Helper

Aug 20th, 2022 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Helper {
  6.    
  7.     public static Random random = new Random();
  8.     public static Scanner scanner = new Scanner(System.in);
  9.    
  10.     //Character
  11.     public static Character getCharacter(Scanner scanner, String inputMessage, String errorMessage) {
  12.         Character characterValue;
  13.         while(true) {
  14.             System.out.println(inputMessage);
  15.             try {
  16.                 characterValue = scanner.nextLine().charAt(0);
  17.                 return characterValue;
  18.             }catch(Exception exception){
  19.                 System.out.println(errorMessage);
  20.                 scanner.nextLine();
  21.             }
  22.         }  
  23.     }
  24.     public static Character getCharacter(Scanner scanner, String inputMessage) {
  25.         return getCharacter(scanner, inputMessage,"Ingrese un carácter válido...");
  26.     }
  27.     public static Character getCharacter(String inputMessage, String errorMessage) {
  28.         return getCharacter(Helper.scanner, inputMessage, errorMessage);
  29.     }
  30.     public static Character getCharacter(String inputMessage) {
  31.         return getCharacter(Helper.scanner, inputMessage, "Ingrese un carácter válido...");
  32.     }
  33.    
  34.     //String
  35.     public static String getString(Scanner scanner, String inputMessage, String errorMessage) {
  36.         String StringValue;
  37.         while(true) {
  38.             System.out.println(inputMessage);
  39.             try {
  40.                 StringValue = scanner.nextLine().substring(0);
  41.                 return StringValue;
  42.             }catch(Exception exception){
  43.                 System.out.println(errorMessage);
  44.                 scanner.nextLine();
  45.             }
  46.         }  
  47.     }
  48.     public static String getString(Scanner scanner, String inputMessage) {
  49.         return getString(scanner, inputMessage,"Ingrese un String válido...");
  50.     }
  51.     public static String getString(String inputMessage, String errorMessage) {
  52.         return getString(Helper.scanner, inputMessage, errorMessage);
  53.     }
  54.     public static String getString(String inputMessage) {
  55.         return getString(Helper.scanner, inputMessage, "Ingrese un carácter válido...");
  56.     }
  57.    
  58.     //Integer
  59.     public static Integer getInteger(Scanner scanner, String inputMessage, String errorMessage) {
  60.         Integer integerValue = 0;
  61.         while(true) {
  62.             System.out.println(inputMessage);
  63.             try {
  64.                 integerValue = Integer.parseInt(scanner.nextLine());
  65.                 return integerValue;
  66.             }catch(Exception exception){
  67.                 System.out.println(errorMessage);
  68.             }
  69.         }
  70.     }
  71.     public static Integer getInteger(Scanner scanner, String inputMessage) {
  72.         return getInteger(scanner, inputMessage, "Ingrese un número entero ");
  73.     }
  74.     public static Integer getInteger(String inputMessage, String errorMessage) {
  75.         return getInteger(Helper.scanner, inputMessage, errorMessage);
  76.     }
  77.     public static Integer getInteger(String inputMessage) {
  78.         return getInteger(Helper.scanner, inputMessage, "Ingrese un número entero ");
  79.     }
  80.     public static Integer getPositiveInt(String inputMessage, String errorMessage) {
  81.         while(true) {
  82.                 int num = getInteger(inputMessage);
  83.                 if (num>0)return num;
  84.                 System.out.println(errorMessage);
  85.         }
  86.     }
  87.     public static Integer getPositiveInt(String inputMessage) {
  88.         return getPositiveInt(inputMessage, "\nError: Ingrese un número positivo ");
  89.     }
  90.    
  91.     //Double
  92.     public static Double getDouble(Scanner scanner, String inputMessage, String errorMessage) {
  93.         Double doubleValue = 0.0;
  94.         while(true) {
  95.             System.out.println(inputMessage);
  96.             try {
  97.                 doubleValue = Double.parseDouble(scanner.nextLine());
  98.                 return doubleValue;
  99.             }catch(Exception exception){
  100.                 System.out.println(errorMessage);
  101.             }
  102.         }
  103.     }
  104.     public static Double getDouble(Scanner scanner, String inputMessage) {
  105.         return getDouble(scanner, inputMessage, "Ingrese un número válido ");
  106.     }
  107.     public static Double getDouble(String inputMessage, String errorMessage) {
  108.         return getDouble(Helper.scanner, inputMessage, errorMessage);
  109.     }
  110.     public static Double getDouble(String inputMessage) {
  111.         return getDouble(Helper.scanner, inputMessage, "Ingrese un número válido ");
  112.     }
  113.     public static Double getPositiveDouble(String inputMessage, String errorMessage) {
  114.         while(true) {
  115.                 double num = getDouble(inputMessage);
  116.                 if (num>0)return num;
  117.                 System.out.println(errorMessage);
  118.         }
  119.     }
  120.     public static Float getPositiveDouble(String inputMessage) {
  121.         return getPositiveFloat(inputMessage, "Error: Ingrese un número positivo ");
  122.     }
  123.    
  124.     //Float
  125.     public static Float getFloat(Scanner scanner, String inputMessage, String errorMessage) {
  126.         Float floatValue = 0f;
  127.         while(true) {
  128.             System.out.println(inputMessage);
  129.             try {
  130.                 floatValue = Float.parseFloat(scanner.nextLine());
  131.                 return floatValue;
  132.             }catch(Exception exception){
  133.                 System.out.println(errorMessage);
  134.             }
  135.         }
  136.     }
  137.     public static Float getFloat(Scanner scanner, String inputMessage) {
  138.         return getFloat(scanner, inputMessage, "Ingrese un número válido ");
  139.     }
  140.     public static Float getFloat(String inputMessage, String errorMessage) {
  141.         return getFloat(Helper.scanner, inputMessage, errorMessage);
  142.     }
  143.     public static Float getFloat(String inputMessage) {
  144.         return getFloat(Helper.scanner, inputMessage, "Ingrese un número válido ");
  145.     }
  146.     public static Float getPositiveFloat(String inputMessage, String errorMessage) {
  147.         while(true) {
  148.                 float num = getFloat(inputMessage);
  149.                 if (num>0)return num;
  150.                 System.out.println(errorMessage);
  151.         }
  152.     }
  153.     public static Float getPositiveFloat(String inputMessage) {
  154.         return getPositiveFloat(inputMessage, "Error: Ingrese un número positivo ");
  155.     }
  156.    
  157.     //Arrays
  158.     static void printTwoDimensionArray(String textBefore, Object[][] array, String textAfter) {
  159.         System.out.print(textBefore);
  160.  
  161.         System.out.print("[[" + array[0][0]);
  162.         for (int j = 1; j < array[0].length; ++j) {
  163.             System.out.print("," + array[0][j]);
  164.         }
  165.         System.out.print("]");
  166.  
  167.         for (int i = 1; i < array.length; ++i) {
  168.             System.out.print(",[" + array[i][0]);
  169.             for (int j = 1; j < array[i].length; ++j) {
  170.                 System.out.print("," + array[i][j]);
  171.             }
  172.             System.out.print("]");
  173.         }
  174.         System.out.print("]");
  175.         System.out.print(textAfter);
  176.     }
  177.    
  178.  
  179.     //region Enum Helpers
  180.  
  181.     // from http://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string
  182.     public static String[] getEnumNames(Class<? extends Enum<?>> e) {
  183.         return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", ");
  184.     }
  185.      
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement