Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.util.Scanner;
  8. import java.io.*;
  9. import java.lang.NumberFormatException;
  10.  
  11. public class Main {
  12.  
  13.  
  14.     static Scanner sc = new Scanner(System.in);
  15.  
  16.  
  17.     static int[] myInsertionSort(int[] arr) {
  18.         int j;
  19.         int temp;
  20.         for (int i = 1; i < arr.length; i++) {
  21.             j = i;
  22.             while (j > 0 && arr[j - 1] > arr[j]) {
  23.                 temp = arr[j];
  24.                 arr[j] = arr[j - 1];
  25.                 arr[j - 1] = temp;
  26.                 j--;
  27.             }
  28.         }
  29.         return arr;
  30.     }
  31.  
  32.     static void outputArray(int[] arr) {
  33.         System.out.println("Resul array");
  34.         for (int i = 0; i < arr.length; i++) {
  35.             if (i != 0)
  36.                 System.out.print(", ");
  37.             System.out.print(arr[i]);
  38.         }
  39.         System.out.println();
  40.     }
  41.  
  42.     static void outputToFile(int[] arr) {
  43.         boolean incorrect;
  44.         sc.nextLine();
  45.         do {
  46.             incorrect = false;
  47.             System.out.println("Enter new file location:");
  48.             String line = sc.nextLine();
  49.             try (PrintWriter writer = new PrintWriter(line, "UTF-8")) {
  50.                 for (int i = 0; i < arr.length; i++)
  51.                     writer.write(arr[i] + "\n");
  52.             } catch (FileNotFoundException e) {
  53.                 System.out.println("File not found, try again");
  54.                 incorrect = true;
  55.             } catch (Exception e) {
  56.                 incorrect = true;
  57.                 e.printStackTrace();
  58.             }
  59.             System.out.println("Successfully recorded");
  60.         } while (incorrect);
  61.     }
  62.  
  63.     static int[] getArrayFromFile() {
  64.         sc.nextLine();
  65.         boolean incorrect;
  66.         String line = "";
  67.         int[] arr = {};
  68.         do {
  69.             incorrect = false;
  70.             System.out.println("Enter file location: ");
  71.             line = sc.nextLine();
  72.             try (BufferedReader br = new BufferedReader(new FileReader(new File(line)))) {
  73.                 line = br.readLine();
  74.                 int size = Integer.parseInt(line);
  75.                 arr = new int[size];
  76.                 line = br.readLine();
  77.                 String[] arrFillings = line.split(" ");
  78.                 for (int i = 0; i < size; i++)
  79.                     arr[i] = Integer.parseInt(arrFillings[i]);
  80.             } catch (NumberFormatException e) {
  81.                 incorrect = true;
  82.                 System.out.println("Invalid data, try again");
  83.             } catch (FileNotFoundException e) {
  84.                 incorrect = true;
  85.                 System.out.println("File with this location not found");
  86.             } catch (Exception e) {
  87.                 incorrect = true;
  88.                 e.printStackTrace();
  89.             }
  90.         } while (incorrect);
  91.         return arr;
  92.     }
  93.  
  94.     static int[] getArrayFromConsole() {
  95.         boolean incorrect;
  96.         String line;
  97.         sc.nextLine();
  98.         do {
  99.             incorrect = false;
  100.             System.out.println("Enter the number of items greater than 2");
  101.             line = sc.nextLine();
  102.             try {
  103.                 if (Integer.parseInt(line) < 2) {
  104.                     incorrect = true;
  105.                     System.out.println("The number of elements must be greater than 2");
  106.                 }
  107.             } catch (Exception e) {
  108.                 incorrect = true;
  109.                 System.out.println("Enter a natural numeric value");
  110.             }
  111.         } while (incorrect);
  112.         int[] arr = new int[Integer.parseInt(line)];
  113.         for (int i = 0; i < arr.length; i++) {
  114.             do {
  115.                 incorrect = false;
  116.                 System.out.println("Enter " + (i + 1) + " element");
  117.                 line = sc.nextLine();
  118.                 try {
  119.                     arr[i] = Integer.parseInt(line);
  120.                 } catch (Exception e) {
  121.                     incorrect = true;
  122.                     System.out.println("Enter an integer value");
  123.                 }
  124.             } while (incorrect);
  125.         }
  126.         return arr;
  127.     }
  128.  
  129.     static int[] chooseInput() {
  130.         char inputType;
  131.         int[] resArr = {};
  132.         boolean incorrect;
  133.         incorrect = true;
  134.         do {
  135.             System.out.println("Enter 'F' to choose input from file\nEnter 'C to choose input from console");
  136.             inputType = sc.next().charAt(0);
  137.             inputType = Character.toUpperCase(inputType);
  138.             switch (inputType) {
  139.                 case 'F':
  140.                     resArr = getArrayFromFile();
  141.                     incorrect = false;
  142.                     break;
  143.                 case 'C':
  144.                     resArr = getArrayFromConsole();
  145.                     incorrect = false;
  146.                     break;
  147.                 default:
  148.                     System.out.println("Your choose incorrect, try again");
  149.                     break;
  150.             }
  151.  
  152.         } while (incorrect);
  153.         return resArr;
  154.     }
  155.  
  156.     public static void main(String[] args) {
  157.         System.out.println("Task: This program sorts numbers by the shell method");
  158.         char output;
  159.         boolean incorrect = true;
  160.         int[] resArr;
  161.         resArr = chooseInput();
  162.         resArr = myInsertionSort(resArr);
  163.         outputArray(resArr);
  164.         do {
  165.             System.out.println("Do u want to save your array to file? (Y/N)");
  166.             output = sc.next().charAt(0);
  167.             output = Character.toUpperCase(output);
  168.             switch (output) {
  169.                 case 'Y':
  170.                     outputToFile(resArr);
  171.                     incorrect = false;
  172.                     break;
  173.                 case 'N':
  174.                     incorrect = false;
  175.                     System.out.println("End of program");
  176.                     break;
  177.                 default:
  178.                     System.out.println("Your choose incorrect, try again");
  179.                     break;
  180.             }
  181.         } while (incorrect);
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement