Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Main {
- private static int[] sequence;
- private static int[] answers;
- private static final int MAX_PEOPLE_VALUE = 10000;
- private static final int MIN_PEOPLE_VALUE = 1;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int numOfElements = 0;
- System.out.println("This program finds palindrome in a sequence of numbers.");
- int inputMethodChoice = chooseInputMethod(scanner);
- answers = new int[2];
- if (inputMethodChoice == 1) {
- numOfElements = setNumOfElements(scanner, MIN_PEOPLE_VALUE, MAX_PEOPLE_VALUE);
- sequence = new int[numOfElements];
- inputDataManually(scanner, numOfElements);
- } else if (inputMethodChoice == 2) {
- String filePath = findFile(scanner);
- numOfElements = inputDataFromFile(filePath);
- }
- findPalindrome(numOfElements);
- outputAnswer(answers[0], answers[1]);
- writeToFile(scanner, answers[0], answers[1]);
- scanner.close();
- }
- private static void outputAnswer(int i, int j) {
- if (i != j) {
- System.out.println("I = [" + (i + 1) + "], J = ["+ (j + 1) + "] - indexes of palindrome.");
- }
- else {
- System.out.println("This array of numbers does not have a palindrome");
- }
- // outputInFile(i, j);
- }
- private static int chooseInputMethod(Scanner scanner) {
- int choice;
- do {
- System.out.println("Choose an option:");
- System.out.println("1. Input sequence manually");
- System.out.println("2. Read sequence from a text file");
- while (!scanner.hasNextInt()) {
- System.out.println("Invalid input. Please enter 1 or 2.");
- scanner.next();
- }
- choice = scanner.nextInt();
- scanner.nextLine();
- if (choice != 1 && choice != 2) {
- System.out.println("Error! You have to input either 1 or 2.");
- }
- } while (choice != 1 && choice != 2);
- return choice;
- }
- private static int setNumOfElements(Scanner scanner, int min, int max) {
- int numOfElements;
- do {
- System.out.println("Enter the number of elements in sequence:");
- while (!scanner.hasNextInt()) {
- System.out.println("Invalid input. Please enter a valid integer.");
- scanner.next();
- }
- numOfElements = scanner.nextInt();
- scanner.nextLine();
- if (numOfElements < min || numOfElements > max) {
- System.out.println("Please enter a value from " + min + " to " + max + "!");
- }
- } while (numOfElements < min || numOfElements > max);
- return numOfElements;
- }
- private static void inputDataManually(Scanner scanner, int numOfElements) {
- for (int i = 0; i < numOfElements; i++) {
- System.out.println("Enter element number " + (i + 1) + " : ");
- int elementValue = scanner.nextInt();
- sequence[i] = elementValue;
- }
- }
- private static boolean isPalindrome(int left, int right) {
- boolean isCorrect;
- if (left >= right) {
- isCorrect = true;
- }
- else {
- isCorrect = ((sequence[left] == sequence[right]) && isPalindrome(left + 1, right - 1));
- }
- return isCorrect;
- }
- private static void findPalindrome(int size) {
- int indexI, indexJ;
- int maxI, maxJ;
- maxI = 0;
- maxJ = 0;
- for (indexI = 0; indexI < size; indexI++) {
- for (indexJ = size - 1; indexJ > indexI; indexJ--) {
- if (isPalindrome(indexI, indexJ)) {
- if ((indexJ - indexI) > (maxJ - maxI)) {
- maxI = indexI;
- maxJ = indexJ;
- }
- }
- }
- }
- answers[0] = maxI;
- answers[1] = maxJ;
- }
- private static String findFile(Scanner scanner) {
- String filePath;
- boolean isCorrect;
- do {
- isCorrect = true;
- System.out.print("Enter path to the file: ");
- filePath = scanner.nextLine();
- File file = new File(filePath);
- if (!file.exists()) {
- isCorrect = false;
- System.out.println("File not found at the specified path.");
- } else if (!filePath.endsWith(".txt")) {
- isCorrect = false;
- System.out.println("Error, wrong file type! File should have .txt extension.");
- }
- } while (!isCorrect);
- System.out.println("File found.");
- return filePath;
- }
- private static int inputDataFromFile(String path) {
- int numOfElements = 0;
- try (Scanner fileScanner = new Scanner(new File(path))) {
- System.out.println("Reading file...");
- String line = fileScanner.nextLine();
- String[] parts = line.split(" ");
- numOfElements = Integer.parseInt(parts[0]);
- sequence = new int[numOfElements];
- line = fileScanner.nextLine();
- parts = line.split(" ");
- for (int i = 0; i < numOfElements; i++) {
- sequence[i] = Integer.parseInt(parts[i]);
- }
- } catch (IOException e) {
- System.out.println("Error reading file!");
- }
- return numOfElements;
- }
- private static void writeToFile(Scanner scanner, int i, int j) {
- try {
- System.out.print("Enter file name: ");
- String fileName = scanner.nextLine();
- fileName += ".txt";
- FileWriter writer = new FileWriter(fileName);
- if (i != j) {
- writer.write("I = [" + (i + 1) + "], J = ["+ (j + 1) + "] - indexes of palindrome.");
- }
- else {
- writer.write("This array of numbers does not have a palindrome");
- }
- writer.close();
- System.out.println("Results written to " + fileName);
- } catch (IOException e) {
- System.out.println("Error writing to file!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement