Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IOException {
- System.out.println("The program sorts the array using the simple insertion method.");
- int source = chooseSource();
- double[] array = inputData(source);
- System.out.println("\nSource array:");
- printArray(array);
- doInsertionSort(array);
- System.out.println("\nFinal array:");
- printArray(array);
- outputData(source, array);
- }
- public static int chooseSource(){
- System.out.println("Choose where to enter data. Enter 1 or 2:\n 1.File\n 2.Console");
- Scanner scan = new Scanner(System.in);
- int choice = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- choice = Integer.parseInt(scan.nextLine());
- }catch (Exception e){
- System.out.println("Enter an integer!");
- isIncorrect = true;
- }
- if (!isIncorrect && choice != 1 && choice != 2){
- System.out.println("Enter 1 or 2!");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return choice;
- }
- public static double[] inputData(int source) throws FileNotFoundException {
- double[] array = new double[0];
- int length;
- switch (source){
- case 1:
- System.out.println("Enter the absolute link to the input file");
- String pathInput = inputPath();
- length = inputLengthFromFile(pathInput);
- array = new double[length];
- inputArrayFromFile(array, pathInput);
- break;
- case 2:
- length = inputLengthFromConsole();
- array = new double[length];
- inputArrayFromConsole(array);
- break;
- }
- return array;
- }
- public static String inputPath(){
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String path;
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()){
- System.out.println("File not found!\nEnter the absolute link to the file");
- isIncorrect = true;
- }
- }while (isIncorrect);
- System.out.println();
- return path;
- }
- public static int inputLengthFromFile(String path) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(path));
- int length;
- boolean flag = true;
- System.out.println("The length of the array is read from file...");
- try {
- length = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("The file has incorrect length!\nEnter the length from console");
- flag = false;
- length = inputLengthFromConsole();
- }
- if (flag && (length < 1 || length > 20)) {
- System.out.println("The file has incorrect length!\nEnter the length from console");
- length = inputLengthFromConsole();
- }
- return length;
- }
- public static void inputArrayFromFile(double[] array, String path) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(path));
- scan.nextLine();
- System.out.println("The array is read from file...");
- try {
- for (int i = 0; i < array.length; i++) {
- array[i] = scan.nextDouble();
- }
- } catch (Exception e) {
- System.out.println("The file has incorrect data!\nEnter the array from console");
- inputArrayFromConsole(array);
- }
- }
- public static int inputLengthFromConsole() {
- Scanner scan = new Scanner(System.in);
- int length = 0;
- boolean isIncorrect;
- System.out.println("Enter the length of the array (from 1 to 20)");
- do {
- isIncorrect = false;
- try {
- length = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Incorrect input!\nEnter the integer");
- isIncorrect = true;
- }
- if (!isIncorrect && (length < 1 || length > 20)) {
- System.out.println("Incorrect input!\nEnter the length of the range from 1 to 20");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return length;
- }
- public static void inputArrayFromConsole(double[] Array) {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- for (int i = 0; i < Array.length; i++) {
- do {
- isIncorrect = false;
- System.out.println("Enter the array element at number " + (i + 1));
- try {
- Array[i] = Double.parseDouble(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Incorrect input!\nEnter the number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- }
- public static void printArray(double[] array) {
- for (double element : array) {
- System.out.print(element + "; ");
- }
- System.out.println("\n");
- }
- public static void doInsertionSort(double[] array){
- for (int i = 0; i < array.length; i++){
- double current = array[i];
- int j = i;
- while(j > 0 && current < array[j - 1]) {
- array[j] = array[j - 1];
- j--;
- }
- array[j] = current;
- viewIterations(array, i, current);
- }
- }
- public static void viewIterations(double[] array, int i, double current){
- System.out.println("Iteration " + (i + 1) + ":");
- System.out.println("Inserted element = " + current);
- System.out.println("Array:");
- printArray(array);
- }
- public static void outputData(int source, double[] array) throws IOException {
- if (source == 1) {
- System.out.println("\nEnter the absolute link to the output file");
- String pathOutput = inputPath();
- outputToFile(array, pathOutput);
- }
- }
- public static void outputToFile(double[] array, String path) throws IOException {
- FileWriter writer = new FileWriter(path);
- for (int i = 0; i < array.length; i++){
- writer.write(array[i] + "; ");
- }
- writer.close();
- System.out.println("The data is successfully recorded in the file");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement