Advertisement
Vanya_Shestakov

laba3.3 (Java)

Oct 19th, 2020 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. package com.company;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         System.out.println("The program sorts the array using the simple insertion method.");
  12.         int source = chooseSource();
  13.         double[] array = inputData(source);
  14.         System.out.println("\nSource array:");
  15.         printArray(array);
  16.         doInsertionSort(array);
  17.         System.out.println("\nFinal array:");
  18.         printArray(array);
  19.         outputData(source, array);
  20.     }  
  21.  
  22.     public static int chooseSource(){
  23.         System.out.println("Choose where to enter data. Enter 1 or 2:\n 1.File\n 2.Console");
  24.         Scanner scan = new Scanner(System.in);
  25.         int choice = 0;
  26.         boolean isIncorrect;
  27.         do {
  28.             isIncorrect = false;
  29.             try {
  30.                 choice = Integer.parseInt(scan.nextLine());
  31.             }catch (Exception e){
  32.                 System.out.println("Enter an integer!");
  33.                 isIncorrect = true;
  34.             }
  35.             if (!isIncorrect && choice != 1 && choice != 2){
  36.                 System.out.println("Enter 1 or 2!");
  37.                 isIncorrect = true;
  38.             }
  39.         }while (isIncorrect);
  40.         return choice;
  41.     }
  42.  
  43.     public static double[] inputData(int source) throws FileNotFoundException {
  44.         double[] array = new double[0];
  45.         int length;
  46.         switch (source){
  47.             case 1:
  48.                 System.out.println("Enter the absolute link to the input file");
  49.                 String pathInput = inputPath();
  50.                 length = inputLengthFromFile(pathInput);
  51.                 array = new double[length];
  52.                 inputArrayFromFile(array, pathInput);
  53.                 break;
  54.             case 2:
  55.                 length = inputLengthFromConsole();
  56.                 array = new double[length];
  57.                 inputArrayFromConsole(array);
  58.                 break;
  59.         }
  60.         return array;
  61.     }
  62.  
  63.     public static String inputPath(){
  64.         Scanner scan = new Scanner(System.in);
  65.         boolean isIncorrect;
  66.         String path;
  67.         do {
  68.             isIncorrect = false;
  69.             path = scan.nextLine();
  70.             File file = new File(path);
  71.  
  72.             if (!file.exists()){
  73.                 System.out.println("File not found!\nEnter the absolute link to the file");
  74.                 isIncorrect = true;
  75.             }
  76.         }while (isIncorrect);
  77.         System.out.println();
  78.         return path;
  79.     }
  80.  
  81.     public static int inputLengthFromFile(String path) throws FileNotFoundException {
  82.         Scanner scan = new Scanner(new File(path));
  83.         int length;
  84.         boolean flag = true;
  85.         System.out.println("The length of the array is read from file...");
  86.         try {
  87.             length = Integer.parseInt(scan.nextLine());
  88.         } catch (Exception e) {
  89.             System.out.println("The file has incorrect length!\nEnter the length from console");
  90.             flag = false;
  91.             length = inputLengthFromConsole();
  92.         }
  93.         if (flag && (length < 1 || length > 20)) {
  94.             System.out.println("The file has incorrect length!\nEnter the length from console");
  95.             length = inputLengthFromConsole();
  96.         }
  97.         return length;
  98.     }
  99.  
  100.     public static void inputArrayFromFile(double[] array, String path) throws FileNotFoundException {
  101.         Scanner scan = new Scanner(new File(path));
  102.         scan.nextLine();
  103.         System.out.println("The array is read from file...");
  104.         try {
  105.             for (int i = 0; i < array.length; i++) {
  106.                 array[i] = scan.nextDouble();
  107.             }
  108.         } catch (Exception e) {
  109.             System.out.println("The file has incorrect data!\nEnter the array from console");
  110.             inputArrayFromConsole(array);
  111.         }
  112.     }
  113.  
  114.     public static int inputLengthFromConsole() {
  115.         Scanner scan = new Scanner(System.in);
  116.         int length = 0;
  117.         boolean isIncorrect;
  118.         System.out.println("Enter the length of the array (from 1 to 20)");
  119.         do {
  120.             isIncorrect = false;
  121.             try {
  122.                 length = Integer.parseInt(scan.nextLine());
  123.             } catch (Exception e) {
  124.                 System.out.println("Incorrect input!\nEnter the integer");
  125.                 isIncorrect = true;
  126.             }
  127.  
  128.             if (!isIncorrect && (length < 1 || length > 20)) {
  129.                 System.out.println("Incorrect input!\nEnter the length of the range from 1 to 20");
  130.                 isIncorrect = true;
  131.             }
  132.         } while (isIncorrect);
  133.         return length;
  134.     }
  135.  
  136.     public static void inputArrayFromConsole(double[] Array) {
  137.         Scanner scan = new Scanner(System.in);
  138.         boolean isIncorrect;
  139.  
  140.         for (int i = 0; i < Array.length; i++) {
  141.             do {
  142.                 isIncorrect = false;
  143.                 System.out.println("Enter the array element at number " + (i + 1));
  144.                 try {
  145.                     Array[i] = Double.parseDouble(scan.nextLine());
  146.                 } catch (Exception e) {
  147.                     System.out.println("Incorrect input!\nEnter the number");
  148.                     isIncorrect = true;
  149.                 }
  150.             } while (isIncorrect);
  151.         }
  152.     }
  153.  
  154.     public static void printArray(double[] array) {
  155.         for (double element : array) {
  156.             System.out.print(element + "; ");
  157.         }
  158.         System.out.println("\n");
  159.     }
  160.  
  161.     public static void doInsertionSort(double[] array){
  162.         for (int i = 0; i < array.length; i++){
  163.             double current = array[i];
  164.             int j = i;
  165.             while(j > 0 && current < array[j - 1]) {
  166.                 array[j] = array[j - 1];
  167.                 j--;
  168.             }
  169.             array[j] = current;
  170.             viewIterations(array, i, current);
  171.         }
  172.     }
  173.  
  174.     public static void viewIterations(double[] array, int i, double current){
  175.         System.out.println("Iteration " + (i + 1) + ":");
  176.         System.out.println("Inserted element = " + current);
  177.         System.out.println("Array:");
  178.         printArray(array);
  179.     }
  180.  
  181.     public static void outputData(int source, double[] array) throws IOException {
  182.         if (source == 1) {
  183.             System.out.println("\nEnter the absolute link to the output file");
  184.             String pathOutput = inputPath();
  185.             outputToFile(array, pathOutput);
  186.         }
  187.     }
  188.  
  189.     public static void outputToFile(double[] array, String path) throws IOException {
  190.         FileWriter writer = new FileWriter(path);
  191.         for (int i = 0; i < array.length; i++){
  192.             writer.write(array[i] + "; ");
  193.         }
  194.         writer.close();
  195.         System.out.println("The data is successfully recorded in the file");
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement