Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- public class Helper {
- public static Random random = new Random();
- public static Scanner scanner = new Scanner(System.in);
- //Character
- public static Character getCharacter(Scanner scanner, String inputMessage, String errorMessage) {
- Character characterValue;
- while(true) {
- System.out.println(inputMessage);
- try {
- characterValue = scanner.nextLine().charAt(0);
- return characterValue;
- }catch(Exception exception){
- System.out.println(errorMessage);
- scanner.nextLine();
- }
- }
- }
- public static Character getCharacter(Scanner scanner, String inputMessage) {
- return getCharacter(scanner, inputMessage,"Ingrese un carácter válido...");
- }
- public static Character getCharacter(String inputMessage, String errorMessage) {
- return getCharacter(Helper.scanner, inputMessage, errorMessage);
- }
- public static Character getCharacter(String inputMessage) {
- return getCharacter(Helper.scanner, inputMessage, "Ingrese un carácter válido...");
- }
- //String
- public static String getString(Scanner scanner, String inputMessage, String errorMessage) {
- String StringValue;
- while(true) {
- System.out.println(inputMessage);
- try {
- StringValue = scanner.nextLine().substring(0);
- return StringValue;
- }catch(Exception exception){
- System.out.println(errorMessage);
- scanner.nextLine();
- }
- }
- }
- public static String getString(Scanner scanner, String inputMessage) {
- return getString(scanner, inputMessage,"Ingrese un String válido...");
- }
- public static String getString(String inputMessage, String errorMessage) {
- return getString(Helper.scanner, inputMessage, errorMessage);
- }
- public static String getString(String inputMessage) {
- return getString(Helper.scanner, inputMessage, "Ingrese un carácter válido...");
- }
- //Integer
- public static Integer getInteger(Scanner scanner, String inputMessage, String errorMessage) {
- Integer integerValue = 0;
- while(true) {
- System.out.println(inputMessage);
- try {
- integerValue = Integer.parseInt(scanner.nextLine());
- return integerValue;
- }catch(Exception exception){
- System.out.println(errorMessage);
- }
- }
- }
- public static Integer getInteger(Scanner scanner, String inputMessage) {
- return getInteger(scanner, inputMessage, "Ingrese un número entero ");
- }
- public static Integer getInteger(String inputMessage, String errorMessage) {
- return getInteger(Helper.scanner, inputMessage, errorMessage);
- }
- public static Integer getInteger(String inputMessage) {
- return getInteger(Helper.scanner, inputMessage, "Ingrese un número entero ");
- }
- public static Integer getPositiveInt(String inputMessage, String errorMessage) {
- while(true) {
- int num = getInteger(inputMessage);
- if (num>0)return num;
- System.out.println(errorMessage);
- }
- }
- public static Integer getPositiveInt(String inputMessage) {
- return getPositiveInt(inputMessage, "\nError: Ingrese un número positivo ");
- }
- //Double
- public static Double getDouble(Scanner scanner, String inputMessage, String errorMessage) {
- Double doubleValue = 0.0;
- while(true) {
- System.out.println(inputMessage);
- try {
- doubleValue = Double.parseDouble(scanner.nextLine());
- return doubleValue;
- }catch(Exception exception){
- System.out.println(errorMessage);
- }
- }
- }
- public static Double getDouble(Scanner scanner, String inputMessage) {
- return getDouble(scanner, inputMessage, "Ingrese un número válido ");
- }
- public static Double getDouble(String inputMessage, String errorMessage) {
- return getDouble(Helper.scanner, inputMessage, errorMessage);
- }
- public static Double getDouble(String inputMessage) {
- return getDouble(Helper.scanner, inputMessage, "Ingrese un número válido ");
- }
- public static Double getPositiveDouble(String inputMessage, String errorMessage) {
- while(true) {
- double num = getDouble(inputMessage);
- if (num>0)return num;
- System.out.println(errorMessage);
- }
- }
- public static Float getPositiveDouble(String inputMessage) {
- return getPositiveFloat(inputMessage, "Error: Ingrese un número positivo ");
- }
- //Float
- public static Float getFloat(Scanner scanner, String inputMessage, String errorMessage) {
- Float floatValue = 0f;
- while(true) {
- System.out.println(inputMessage);
- try {
- floatValue = Float.parseFloat(scanner.nextLine());
- return floatValue;
- }catch(Exception exception){
- System.out.println(errorMessage);
- }
- }
- }
- public static Float getFloat(Scanner scanner, String inputMessage) {
- return getFloat(scanner, inputMessage, "Ingrese un número válido ");
- }
- public static Float getFloat(String inputMessage, String errorMessage) {
- return getFloat(Helper.scanner, inputMessage, errorMessage);
- }
- public static Float getFloat(String inputMessage) {
- return getFloat(Helper.scanner, inputMessage, "Ingrese un número válido ");
- }
- public static Float getPositiveFloat(String inputMessage, String errorMessage) {
- while(true) {
- float num = getFloat(inputMessage);
- if (num>0)return num;
- System.out.println(errorMessage);
- }
- }
- public static Float getPositiveFloat(String inputMessage) {
- return getPositiveFloat(inputMessage, "Error: Ingrese un número positivo ");
- }
- //Arrays
- static void printTwoDimensionArray(String textBefore, Object[][] array, String textAfter) {
- System.out.print(textBefore);
- System.out.print("[[" + array[0][0]);
- for (int j = 1; j < array[0].length; ++j) {
- System.out.print("," + array[0][j]);
- }
- System.out.print("]");
- for (int i = 1; i < array.length; ++i) {
- System.out.print(",[" + array[i][0]);
- for (int j = 1; j < array[i].length; ++j) {
- System.out.print("," + array[i][j]);
- }
- System.out.print("]");
- }
- System.out.print("]");
- System.out.print(textAfter);
- }
- //region Enum Helpers
- // from http://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string
- public static String[] getEnumNames(Class<? extends Enum<?>> e) {
- return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement