Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.NoSuchElementException;
- import java.io.*;
- public class Main {
- private static final Scanner scan = new Scanner(System.in);
- public static int choiceCheck() {
- int choice;
- choice = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- choice = Integer.parseInt(scan.nextLine());
- } catch (Exception err) {
- System.out.println("Error! Input a number");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice < 1 || choice > 2)) {
- System.out.println("Error! Input 1 or 2");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- public static int inputCheck() {
- boolean isIncorrect;
- int n;
- n = 0;
- System.out.println("Input n");
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scan.nextLine());
- } catch (Exception err) {
- System.out.println("Error! Input a number");
- isIncorrect = true;
- }
- if (!isIncorrect && (n < 1)) {
- System.out.println("Error! Input a number greater than 0");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return n;
- }
- public static int[] inputCheckArray(int n, int[] arr) {
- int i;
- boolean isIncorrect;
- for (i = 0; i < n; i++) {
- do {
- isIncorrect = false;
- try {
- arr[i] = Integer.parseInt(scan.nextLine());
- } catch (Exception err) {
- System.out.println("Error! Input a number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- return arr;
- }
- public static String checkInputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- System.out.println("Input path to the file:");
- isIncorrect = false;
- path = scan.nextLine();
- File inputFile = new File(path);
- if (!inputFile.exists()) {
- isIncorrect = true;
- System.out.println("Could not find the file");
- } else if ((!inputFile.canRead()) || (!inputFile.canWrite())) {
- isIncorrect = true;
- System.out.println("Could not open the file");
- }
- } while (isIncorrect);
- return path;
- }
- public static int[] fileCheckArray(int n) {
- String path;
- boolean isIncorrect;
- int i, j, n1, c;
- int[] arr;
- do {
- isIncorrect = false;
- path = checkInputFilePath();
- File inputFile = new File(path);
- Scanner scan2 = null;
- try {
- scan2 = new Scanner(inputFile);
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- }
- try {
- n = scan2.nextInt();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.print("The data is incorrect");
- }
- if ((!isIncorrect) && (n < 2)) {
- isIncorrect = true;
- System.out.println("The data is incorrect");
- }
- arr = new int[n];
- try {
- for (i = 0; i < n; i++) {
- arr[i] = scan2.nextInt();
- }
- } catch (NoSuchElementException e) {
- System.out.println("The data is incorrect\n");
- isIncorrect = true;
- scan2.close();
- }
- } while (isIncorrect);
- return arr;
- }
- public static int[] inputChoice(int n) {
- int choice;
- int[] arr;
- System.out.println("Choose input option:\n1.Input through console\n2.Input through file");
- choice = choiceCheck();
- if (choice == 1) {
- n = inputCheck();
- arr = new int[n];
- arr = inputCheckArray(n, arr);
- } else {
- arr = fileCheckArray(n);
- }
- return arr;
- }
- public static String checkOutputFilePath() {
- String path;
- System.out.println("Input file path and the name of the file for\nexample С:\\Projects\\Number\\FileName.txt. If the\nfile does not exist, then it will be created\nautomatically in the given directory");
- path = scan.nextLine();
- File outputFile = new File(path);
- if (outputFile.isFile()) {
- if (!outputFile.canRead()) {
- System.out.println("Could not open the file");
- }
- }
- return path;
- }
- public static int findMax(int[] arr, int max, int i) {
- if (i == 0){
- return max;
- }
- else{
- if (max < arr[i]){
- max = arr[i];
- return findMax(arr, arr[i], i-1);
- }else {
- return findMax(arr, max, i - 1);
- }
- }
- }
- public static void outputMax(int max) {
- System.out.println(max);
- }
- public static void outputFile(int max) {
- String path;
- path = checkOutputFilePath();
- try (PrintWriter pw = new PrintWriter((path))) {
- pw.print(max);
- } catch (IOException ex) {
- System.out.println("Unsuccessful output");
- }
- }
- public static void outputChoice(int max) {
- int choice;
- System.out.println("Choose output option:\n1.Output through console\n2.Output through file");
- choice = choiceCheck();
- if (choice == 1) {
- outputMax(max);
- } else {
- outputFile(max);
- }
- scan.close();
- }
- public static void main(String[] args) {
- int[] arr;
- int n, max;
- n = 0;
- arr = inputChoice(n);
- n = arr.length-1;
- max = findMax(arr, arr[n], n);
- outputChoice(max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement