Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.Scanner;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- public static void main(String[] args){
- System.out.println("Welcome to the program that checks is a given number a palindrome.");
- String inputSource = getSource("entering the number");
- int number = getNumber(inputSource);
- int reverse = getReverse(number);
- String answer = getAnswer(number,reverse);
- String outputSource = getSource("output");
- scan.close();
- output(outputSource,answer);
- }
- static String getSource(String what){
- System.out.print("Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ");
- String choice = scan.nextLine();
- while ((!choice.equals("1")) && (!choice.equals("2"))){
- System.out.print("Incorrect input!!! Select the source for " + what + ":\n1:Console\n2:File\nEnter 1 or 2: ");
- choice = scan.nextLine();
- }
- return choice;
- }
- static String getInPath(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (Files.notExists(Path.of(path))) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- static String getOutPath(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- static void outputToFile(String path, String answer){
- try{
- FileWriter writer = new FileWriter(path);
- writer.write(answer);
- writer.flush();
- System.out.println("Complete!!!");
- }
- catch(IOException ex){
- System.out.println("Incorrect file content!!!");
- }
- }
- static int getNumberFromConsole() {
- boolean isCorrect;
- int number = 0;
- System.out.print("Enter number: ");
- do {
- isCorrect = false;
- try {
- number = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!!\nEnter number: ");
- isCorrect = true;
- }
- if (!isCorrect && (number < 1)) {
- System.out.println("The number must be more than 0\nEnter number: ");
- isCorrect = true;
- }
- } while (isCorrect);
- return number;
- }
- static int getNumberFromFile(String path){
- int number = 0;
- String line;
- int lineCounter = 0;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(path));
- while ((line = reader.readLine()) != null) {
- lineCounter++;
- if (lineCounter == 1) {
- try {
- number = Integer.parseInt(line);
- } catch (Exception e) {
- System.out.println("Incorrect file content!!!");
- }
- }
- }
- reader.close();
- }catch (IOException e) {
- System.out.println("Input/Output error!!!");
- number = 0;
- }
- if ((number > 0) && (lineCounter > 1)){
- System.out.println("Incorrect file content!!!");
- number = 0;
- }
- return number;
- }
- static int getNumber(String source){
- int number;
- String InPath;
- if (source.equals("1")) {
- number = getNumberFromConsole();
- } else {
- InPath = getInPath();
- number = getNumberFromFile(InPath);
- while (number == 0) {
- InPath = getInPath();
- number = getNumberFromFile(InPath);
- }
- }
- return number;
- }
- static int getReverse (int number) {
- int result = 0;
- while (number > 0){
- result = result * 10 + number % 10;
- number /= 10;
- }
- return result;
- }
- static String getAnswer (int number, int reverse) {
- if (number == reverse)
- return "Number is palindrome";
- else
- return "Number isn't palindrome";
- }
- static void output(String source, String answer){
- String outPath;
- if (source.equals("1")) {
- System.out.println(answer);
- } else{
- outPath = getOutPath();
- outputToFile(outPath, answer);
- }
- }
- }
Add Comment
Please, Sign In to add comment