Advertisement
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;
- import java.util.regex.PatternSyntaxException;
- 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 sequence of numbers non-growing");
- byte inputSource = takeSource("entering the sequence of numbers");
- double[] sequence = takeSequence(inputSource);
- String answer = findAnswer(sequence);
- byte outputSource = takeSource("output the answer");
- output(outputSource,answer);
- scan.close();
- }
- static byte takeSource(final String purpose){
- System.out.print("Select the source for " + purpose + ":\n1:Console\n2:File\nEnter 1 or 2: ");
- boolean isIncorrect;
- byte choice = 0;
- final byte CONSOLE = 1;
- final byte FILE = 2;
- do {
- isIncorrect = false;
- try {
- choice = Byte.parseByte(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!! Select the source for " + purpose + ":\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
- System.out.print("Incorrect input!!! Select the source for " + purpose + ":\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- static String takeInPath(){
- 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 takeOutPath(){
- 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 double[] takeSequenceFromConsole() {
- boolean isIncorrect;
- int size = 0;
- final int MIN_SIZE = 2;
- System.out.print("Enter size: ");
- do {
- isIncorrect = false;
- try {
- size = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!!\nEnter size: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (size < MIN_SIZE)) {
- System.out.println("The size must be higher than 1\nEnter size: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- double[] sequence = new double[size];
- for (int i = 0; i < size; i++){
- System.out.print("Enter element " + (i + 1) + ": ");
- do {
- isIncorrect = false;
- try {
- sequence[i] = Double.parseDouble(scan.nextLine());
- }
- catch(Exception e) {
- System.out.print("Incorrect input!!!\nEnter element " + (i + 1) + ": ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- }
- return sequence;
- }
- static double[] takeSequenceFromFile(final String path){
- int size = 0;
- final int MIN_SIZE = 2;
- int lineCounter = 0;
- final int SIZE_LINE_NUMBER = 1;
- final int SEQUENCE_LINE_NUMBER = 2;
- String line;
- String[] stringSequence = null;
- boolean isCorrect = true;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(path));
- while ((isCorrect) && ((line = reader.readLine()) != null)) {
- lineCounter++;
- if (lineCounter == SIZE_LINE_NUMBER){
- try {
- size = Integer.parseInt(line);
- } catch (Exception e) {
- System.out.println("Incorrect file content!!! Incorrect information in the size line!!!");
- isCorrect = false;
- }
- if (isCorrect && (size < MIN_SIZE)) {
- System.out.println("Incorrect file content!!! The size must be higher than 1!!!");
- isCorrect = false;
- }
- }
- if (lineCounter == SEQUENCE_LINE_NUMBER){
- try {
- stringSequence = line.split(" ");
- } catch (PatternSyntaxException e){
- System.out.println("Incorrect file content!!! Incorrect information in the sequence line!!!");
- isCorrect = false;
- }
- }
- }
- reader.close();
- }catch (IOException e) {
- System.out.println("Input/Output error!!!");
- isCorrect = false;
- }
- double[] sequence = null;
- if ((isCorrect) && ((stringSequence.length != size) || (lineCounter > SEQUENCE_LINE_NUMBER))){
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }else{
- sequence = new double[size];
- for (int i = 0;i < size;i++){
- try {
- sequence[i] = Double.parseDouble(stringSequence[i]);
- } catch (Exception e) {
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }
- }
- }
- if(isCorrect) {
- System.out.print("Sequence is: ");
- for (int i = 0; i < size; i++) {
- System.out.print(sequence[i] + "; ");
- }
- System.out.println();
- return sequence;
- }else{
- return null;
- }
- }
- static double[] takeSequence(final byte source){
- String inPath;
- double[] sequence;
- if (source == 1) {
- sequence = takeSequenceFromConsole().clone();
- } else {
- inPath = takeInPath();
- sequence = takeSequenceFromFile(inPath);
- while (sequence == null) {
- inPath = takeInPath();
- sequence = takeSequenceFromFile(inPath);
- }
- }
- return sequence;
- }
- static String findAnswer (final double[] sequence) {
- int i = 0;
- boolean isCorrect;
- do{
- isCorrect = sequence[i] >= sequence[i + 1];
- i++;
- }while ((isCorrect) && (i < sequence.length - 1));
- if (isCorrect) {
- return "The sequence is non-growing";
- }else {
- return "The sequence isn't non-growing";
- }
- }
- static void outputToFile(final String path,final 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 void output(final byte source,final String answer){
- String outPath;
- if (source == 1) {
- System.out.println(answer);
- } else{
- outPath = takeOutPath();
- outputToFile(outPath, answer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement