Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.Scanner;
- import java.io.*;
- import java.lang.NumberFormatException;
- public class Main {
- static Scanner sc = new Scanner(System.in);
- static int[] myInsertionSort(int[] arr) {
- int j;
- int temp;
- for (int i = 1; i < arr.length; i++) {
- j = i;
- while (j > 0 && arr[j - 1] > arr[j]) {
- temp = arr[j];
- arr[j] = arr[j - 1];
- arr[j - 1] = temp;
- j--;
- }
- }
- return arr;
- }
- static void outputArray(int[] arr) {
- System.out.println("Resul array");
- for (int i = 0; i < arr.length; i++) {
- if (i != 0)
- System.out.print(", ");
- System.out.print(arr[i]);
- }
- System.out.println();
- }
- static void outputToFile(int[] arr) {
- boolean incorrect;
- sc.nextLine();
- do {
- incorrect = false;
- System.out.println("Enter new file location:");
- String line = sc.nextLine();
- try (PrintWriter writer = new PrintWriter(line, "UTF-8")) {
- for (int i = 0; i < arr.length; i++)
- writer.write(arr[i] + "\n");
- } catch (FileNotFoundException e) {
- System.out.println("File not found, try again");
- incorrect = true;
- } catch (Exception e) {
- incorrect = true;
- e.printStackTrace();
- }
- System.out.println("Successfully recorded");
- } while (incorrect);
- }
- static int[] getArrayFromFile() {
- sc.nextLine();
- boolean incorrect;
- String line = "";
- int[] arr = {};
- do {
- incorrect = false;
- System.out.println("Enter file location: ");
- line = sc.nextLine();
- try (BufferedReader br = new BufferedReader(new FileReader(new File(line)))) {
- line = br.readLine();
- int size = Integer.parseInt(line);
- arr = new int[size];
- line = br.readLine();
- String[] arrFillings = line.split(" ");
- for (int i = 0; i < size; i++)
- arr[i] = Integer.parseInt(arrFillings[i]);
- } catch (NumberFormatException e) {
- incorrect = true;
- System.out.println("Invalid data, try again");
- } catch (FileNotFoundException e) {
- incorrect = true;
- System.out.println("File with this location not found");
- } catch (Exception e) {
- incorrect = true;
- e.printStackTrace();
- }
- } while (incorrect);
- return arr;
- }
- static int[] getArrayFromConsole() {
- boolean incorrect;
- String line;
- sc.nextLine();
- do {
- incorrect = false;
- System.out.println("Enter the number of items greater than 2");
- line = sc.nextLine();
- try {
- if (Integer.parseInt(line) < 2) {
- incorrect = true;
- System.out.println("The number of elements must be greater than 2");
- }
- } catch (Exception e) {
- incorrect = true;
- System.out.println("Enter a natural numeric value");
- }
- } while (incorrect);
- int[] arr = new int[Integer.parseInt(line)];
- for (int i = 0; i < arr.length; i++) {
- do {
- incorrect = false;
- System.out.println("Enter " + (i + 1) + " element");
- line = sc.nextLine();
- try {
- arr[i] = Integer.parseInt(line);
- } catch (Exception e) {
- incorrect = true;
- System.out.println("Enter an integer value");
- }
- } while (incorrect);
- }
- return arr;
- }
- static int[] chooseInput() {
- char inputType;
- int[] resArr = {};
- boolean incorrect;
- incorrect = true;
- do {
- System.out.println("Enter 'F' to choose input from file\nEnter 'C to choose input from console");
- inputType = sc.next().charAt(0);
- inputType = Character.toUpperCase(inputType);
- switch (inputType) {
- case 'F':
- resArr = getArrayFromFile();
- incorrect = false;
- break;
- case 'C':
- resArr = getArrayFromConsole();
- incorrect = false;
- break;
- default:
- System.out.println("Your choose incorrect, try again");
- break;
- }
- } while (incorrect);
- return resArr;
- }
- public static void main(String[] args) {
- System.out.println("Task: This program sorts numbers by the shell method");
- char output;
- boolean incorrect = true;
- int[] resArr;
- resArr = chooseInput();
- resArr = myInsertionSort(resArr);
- outputArray(resArr);
- do {
- System.out.println("Do u want to save your array to file? (Y/N)");
- output = sc.next().charAt(0);
- output = Character.toUpperCase(output);
- switch (output) {
- case 'Y':
- outputToFile(resArr);
- incorrect = false;
- break;
- case 'N':
- incorrect = false;
- System.out.println("End of program");
- break;
- default:
- System.out.println("Your choose incorrect, try again");
- break;
- }
- } while (incorrect);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement