Egor_Vakar

lab2.3(java)

Oct 8th, 2021 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.util.Scanner;
  6. public class Main {
  7.     static Scanner scan = new Scanner(System.in);
  8.     public static void main(String[] args){
  9.         System.out.println("Welcome to the program that checks is a given number a palindrome.");
  10.         String inputSource = getSource("entering the number");
  11.         int number = getNumber(inputSource);
  12.         int reverse = getReverse(number);
  13.         String answer = getAnswer(number,reverse);
  14.         String outputSource = getSource("output");
  15.         scan.close();
  16.         output(outputSource,answer);
  17.     }
  18.     static String getSource(String what){
  19.         System.out.print("Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ");
  20.         String choice = scan.nextLine();
  21.         while ((!choice.equals("1")) && (!choice.equals("2"))){
  22.             System.out.print("Incorrect input!!! Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ");
  23.             choice = scan.nextLine();
  24.         }
  25.         return choice;
  26.     }
  27.     static String getInPath(){
  28.         String path;
  29.         boolean isIncorrect;
  30.         System.out.print("Enter file path: ");
  31.         do {
  32.             isIncorrect = false;
  33.             path = scan.nextLine();
  34.             if (Files.notExists(Path.of(path))) {
  35.                 System.out.print("File is not found\nEnter file path: ");
  36.                 isIncorrect = true;
  37.             }
  38.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  39.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  40.                 isIncorrect = true;
  41.             }
  42.         }while (isIncorrect);
  43.         return path;
  44.     }
  45.     static String getOutPath(){
  46.         String path;
  47.         boolean isIncorrect;
  48.         System.out.print("Enter file path: ");
  49.         do {
  50.             isIncorrect = false;
  51.             path = scan.nextLine();
  52.             if (!path.endsWith(".txt")) {
  53.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  54.                 isIncorrect = true;
  55.             }
  56.         }while (isIncorrect);
  57.         return path;
  58.     }
  59.     static void outputToFile(String path, String answer){
  60.         try{
  61.             FileWriter writer = new FileWriter(path);
  62.             writer.write(answer);
  63.             writer.flush();
  64.             System.out.println("Complete!!!");
  65.         }
  66.         catch(IOException ex){
  67.             System.out.println("Incorrect file content!!!");
  68.         }
  69.     }
  70.     static int getNumberFromConsole() {
  71.         boolean isCorrect;
  72.         int number = 0;
  73.         System.out.print("Enter number: ");
  74.         do {
  75.             isCorrect = false;
  76.             try {
  77.                 number = Integer.parseInt(scan.nextLine());
  78.             } catch (Exception e) {
  79.                 System.out.print("Incorrect input!!!\nEnter number: ");
  80.                 isCorrect = true;
  81.             }
  82.             if (!isCorrect && (number < 1)) {
  83.                 System.out.println("The number must be more than 0\nEnter number: ");
  84.                 isCorrect = true;
  85.             }
  86.         } while (isCorrect);
  87.         return number;
  88.     }
  89.     static int getNumberFromFile(String path){
  90.         int number = 0;
  91.         String line;
  92.         int lineCounter = 0;
  93.         try {
  94.             BufferedReader reader = new BufferedReader(new FileReader(path));
  95.             while ((line = reader.readLine()) != null) {
  96.                 lineCounter++;
  97.                     if (lineCounter == 1) {
  98.                         try {
  99.                             number = Integer.parseInt(line);
  100.                         } catch (Exception e) {
  101.                             System.out.println("Incorrect file content!!!");
  102.                         }
  103.                     }
  104.                 }
  105.                 reader.close();
  106.         }catch (IOException e) {
  107.             System.out.println("Input/Output error!!!");
  108.             number = 0;
  109.         }
  110.         if ((number > 0) && (lineCounter > 1)){
  111.             System.out.println("Incorrect file content!!!");
  112.             number = 0;
  113.         }
  114.         return number;
  115.     }
  116.     static int getNumber(String source){
  117.         int number;
  118.         String InPath;
  119.         if (source.equals("1")) {
  120.             number = getNumberFromConsole();
  121.         } else {
  122.             InPath = getInPath();
  123.             number = getNumberFromFile(InPath);
  124.             while (number == 0) {
  125.                 InPath = getInPath();
  126.                 number = getNumberFromFile(InPath);
  127.             }
  128.         }
  129.         return number;
  130.     }
  131.     static int getReverse (int number) {
  132.         int result = 0;
  133.         while (number > 0){
  134.             result = result * 10 + number % 10;
  135.             number /= 10;
  136.         }
  137.         return result;
  138.     }
  139.     static String getAnswer  (int number, int reverse) {
  140.         if (number == reverse)
  141.             return "Number is palindrome";
  142.         else
  143.             return "Number isn't palindrome";
  144.     }
  145.     static void output(String source, String answer){
  146.         String outPath;
  147.         if (source.equals("1")) {
  148.             System.out.println(answer);
  149.         } else{
  150.             outPath = getOutPath();
  151.             outputToFile(outPath, answer);
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment