Advertisement
Vanilla_Fury

choose from menu - java

Apr 3rd, 2022 (edited)
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class MyMenu {
  7.     private static Scanner scConsole = new Scanner(System.in);
  8.  
  9.     public static short choose(String sQuestion, String[] Options) { // не больше 9-ти вариантов ответа
  10.         short nChoice;
  11.         String answer;
  12.         StringBuilder optionsDigits = new StringBuilder();
  13.  
  14.         System.out.println(sQuestion + "\n\tВарианты: ");
  15.         for (int i = 0; i < Options.length; i++) {
  16.             System.out.println("\t" + (i + 1) + " - " + Options[i]);
  17.             optionsDigits.append(i + 1);
  18.         }
  19.  
  20.         answer = getAnythingFromConsole("", "^[" + optionsDigits + "]$", "Нужно ввести " +
  21.                 "цифру (одну из предложенных).");
  22.         nChoice = (short) (Short.parseShort(answer) - 1);
  23.  
  24.         return nChoice;
  25.     }
  26.  
  27.     public static String getAnythingFromConsole(String Question, String regEx, String clarification) {
  28.         String sInput;
  29.         String sOutput;
  30.         String stringIfNothingIsFoundInString = "";
  31.         boolean isIncorrect;
  32.         boolean nothingIsFoundIsNotAllowed = findRegEx(regEx, "\\^\\$", "")[0].equals("");
  33.         if (!nothingIsFoundIsNotAllowed) {
  34.             stringIfNothingIsFoundInString = regEx + "++++++";
  35.         }
  36.  
  37.         if (!Question.equals("")) {
  38.             System.out.println(Question);
  39.         }
  40.  
  41.         do {
  42.             sInput = scConsole.nextLine().trim();
  43.             sOutput = findRegEx(sInput, regEx, stringIfNothingIsFoundInString)[0];
  44.             if (sOutput.equals(stringIfNothingIsFoundInString)) {
  45.                 System.err.println("Данные введены неверно. " + clarification + "\nПовторите попытку:");
  46.                 isIncorrect = true;
  47.             } else {
  48.                 isIncorrect = false;
  49.             }
  50.         } while (isIncorrect);
  51.  
  52.         System.out.println();
  53.         return sOutput;
  54.     }
  55.  
  56.     private static String[] findRegEx(String sInput, String regEx, String outputIfNothingFound) {
  57.         ArrayList<String> arrStringOutput = new ArrayList<>();
  58.         Pattern pattern = Pattern.compile(regEx);
  59.         Matcher matcher = pattern.matcher(sInput);
  60.  
  61.         if (matcher.find()) {
  62.             do {
  63.                 arrStringOutput.add(matcher.group());
  64.             } while (matcher.find());
  65.         } else
  66.             arrStringOutput.add(outputIfNothingFound);
  67.  
  68.         return arrStringOutput.toArray(new String[0]);
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement