Advertisement
Sauka1337

4.5

May 19th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6. public class Main {
  7.     private static int[] sequence;
  8.     private static int[] answers;
  9.     private static final int MAX_PEOPLE_VALUE = 10000;
  10.     private static final int MIN_PEOPLE_VALUE = 1;
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         Scanner scanner = new Scanner(System.in);
  15.         int numOfElements = 0;
  16.  
  17.         System.out.println("This program finds palindrome in a sequence of numbers.");
  18.         int inputMethodChoice = chooseInputMethod(scanner);
  19.  
  20.         answers = new int[2];
  21.  
  22.         if (inputMethodChoice == 1) {
  23.             numOfElements = setNumOfElements(scanner, MIN_PEOPLE_VALUE, MAX_PEOPLE_VALUE);
  24.             sequence = new int[numOfElements];
  25.             inputDataManually(scanner, numOfElements);
  26.         } else if (inputMethodChoice == 2) {
  27.             String filePath = findFile(scanner);
  28.             numOfElements = inputDataFromFile(filePath);
  29.         }
  30.  
  31.         findPalindrome(numOfElements);
  32.         outputAnswer(answers[0], answers[1]);
  33.         writeToFile(scanner, answers[0], answers[1]);
  34.  
  35.         scanner.close();
  36.  
  37.     }
  38.  
  39.     private static void outputAnswer(int i, int j) {
  40.         if (i != j) {
  41.             System.out.println("I = [" + (i + 1) + "], J = ["+ (j + 1) + "] - indexes of palindrome.");
  42.         }
  43.         else {
  44.             System.out.println("This array of numbers does not have a palindrome");
  45.         }
  46.  
  47. //        outputInFile(i, j);
  48.     }
  49.     private static int chooseInputMethod(Scanner scanner) {
  50.         int choice;
  51.         do {
  52.             System.out.println("Choose an option:");
  53.             System.out.println("1. Input sequence manually");
  54.             System.out.println("2. Read sequence from a text file");
  55.  
  56.             while (!scanner.hasNextInt()) {
  57.                 System.out.println("Invalid input. Please enter 1 or 2.");
  58.                 scanner.next();
  59.             }
  60.             choice = scanner.nextInt();
  61.             scanner.nextLine();
  62.             if (choice != 1 && choice != 2) {
  63.                 System.out.println("Error! You have to input either 1 or 2.");
  64.             }
  65.         } while (choice != 1 && choice != 2);
  66.         return choice;
  67.     }
  68.  
  69.     private static int setNumOfElements(Scanner scanner, int min, int max) {
  70.         int numOfElements;
  71.         do {
  72.             System.out.println("Enter the number of elements in sequence:");
  73.             while (!scanner.hasNextInt()) {
  74.                 System.out.println("Invalid input. Please enter a valid integer.");
  75.                 scanner.next();
  76.             }
  77.             numOfElements = scanner.nextInt();
  78.             scanner.nextLine();
  79.             if (numOfElements < min || numOfElements > max) {
  80.                 System.out.println("Please enter a value from " + min + " to " + max + "!");
  81.             }
  82.         } while (numOfElements < min || numOfElements > max);
  83.         return numOfElements;
  84.     }
  85.  
  86.     private static void inputDataManually(Scanner scanner, int numOfElements) {
  87.         for (int i = 0; i < numOfElements; i++) {
  88.             System.out.println("Enter element number " + (i + 1) + " : ");
  89.             int elementValue = scanner.nextInt();
  90.             sequence[i] = elementValue;
  91.         }
  92.     }
  93.     private static boolean isPalindrome(int left, int right) {
  94.         boolean isCorrect;
  95.  
  96.         if (left >= right) {
  97.             isCorrect = true;
  98.         }
  99.         else {
  100.             isCorrect = ((sequence[left] == sequence[right]) && isPalindrome(left + 1, right - 1));
  101.         }
  102.  
  103.         return isCorrect;
  104.     }
  105.  
  106.     private static void findPalindrome(int size) {
  107.         int indexI, indexJ;
  108.         int maxI, maxJ;
  109.  
  110.         maxI = 0;
  111.         maxJ = 0;
  112.         for (indexI = 0; indexI < size; indexI++) {
  113.             for (indexJ = size - 1; indexJ > indexI; indexJ--) {
  114.                 if (isPalindrome(indexI, indexJ)) {
  115.                     if ((indexJ - indexI) > (maxJ - maxI)) {
  116.                         maxI = indexI;
  117.                         maxJ = indexJ;
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         answers[0] = maxI;
  123.         answers[1] = maxJ;
  124.     }
  125.  
  126.     private static String findFile(Scanner scanner) {
  127.         String filePath;
  128.         boolean isCorrect;
  129.         do {
  130.             isCorrect = true;
  131.             System.out.print("Enter path to the file: ");
  132.             filePath = scanner.nextLine();
  133.             File file = new File(filePath);
  134.             if (!file.exists()) {
  135.                 isCorrect = false;
  136.                 System.out.println("File not found at the specified path.");
  137.             } else if (!filePath.endsWith(".txt")) {
  138.                 isCorrect = false;
  139.                 System.out.println("Error, wrong file type! File should have .txt extension.");
  140.             }
  141.         } while (!isCorrect);
  142.         System.out.println("File found.");
  143.         return filePath;
  144.     }
  145.     private static int inputDataFromFile(String path) {
  146.         int numOfElements = 0;
  147.         try (Scanner fileScanner = new Scanner(new File(path))) {
  148.             System.out.println("Reading file...");
  149.  
  150.             String line = fileScanner.nextLine();
  151.             String[] parts = line.split(" ");
  152.  
  153.             numOfElements = Integer.parseInt(parts[0]);
  154.             sequence = new int[numOfElements];
  155.  
  156.             line = fileScanner.nextLine();
  157.             parts = line.split(" ");
  158.  
  159.             for (int i = 0; i < numOfElements; i++) {
  160.                 sequence[i] = Integer.parseInt(parts[i]);
  161.             }
  162.         } catch (IOException e) {
  163.             System.out.println("Error reading file!");
  164.         }
  165.         return numOfElements;
  166.     }
  167.     private static void writeToFile(Scanner scanner, int i, int j) {
  168.         try {
  169.             System.out.print("Enter file name: ");
  170.             String fileName = scanner.nextLine();
  171.             fileName += ".txt";
  172.             FileWriter writer = new FileWriter(fileName);
  173.  
  174.             if (i != j) {
  175.                 writer.write("I = [" + (i + 1) + "], J = ["+ (j + 1) + "] - indexes of palindrome.");
  176.             }
  177.             else {
  178.                 writer.write("This array of numbers does not have a palindrome");
  179.             }
  180.  
  181.             writer.close();
  182.  
  183.             System.out.println("Results written to " + fileName);
  184.         } catch (IOException e) {
  185.             System.out.println("Error writing to file!");
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement