Advertisement
Eugene0091

Laba2.2 java

Nov 3rd, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.51 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5.  
  6. public class Main {
  7.     public static Scanner scan = new Scanner(System.in);
  8.     public final static String CHAR_MISTAKE_MESSAGE = "Error! Enter a number.\n";
  9.     public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! File name can not be found. Please try again.";
  10.     public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to open this file. Please check the file and try again.";
  11.  
  12.     public enum typeOfInput {
  13.         CHOOSEKEYBOARD, CHOOSEFILE
  14.     }
  15.  
  16.     public enum typeOfOutput {
  17.         YES, NO
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         final String THEME_MESSAGE = "This program finds pairs of friendly numbers in a given range.";
  22.         final String DOTTED_LINE = "--------------------------------------------------------------------\n";
  23.         int range = 0;
  24.         int[] friendlyNumbers = new int[0];
  25.         System.out.print(THEME_MESSAGE);
  26.         System.out.print(DOTTED_LINE);
  27.         typeOfInput Input = chooseInput();
  28.         switch (Input) {
  29.             case CHOOSEKEYBOARD:
  30.                 System.out.print("Please enter an upper limit (from 284 to 10,000) for the friendly number search range: ");
  31.                 range = checkInputNumber(284, 10000);
  32.                 break;
  33.             case CHOOSEFILE:
  34.                 range = readRangeFromFile();
  35.                 System.out.println("Range: " + range);
  36.                 break;
  37.         }
  38.         friendlyNumbers = searchFriendlyNumber(range + 1);
  39.         outputConsole(friendlyNumbers);
  40.         typeOfOutput Output = chooseOutput();
  41.         switch (Output) {
  42.             case YES:
  43.                 writeToFile(friendlyNumbers);
  44.                 break;
  45.             case NO:
  46.                 break;
  47.         }
  48.         System.out.print(DOTTED_LINE);
  49.         System.out.println("Program completed");
  50.     }
  51.  
  52.     public static int checkInputNumber(int min, int max) {
  53.         boolean isCorrect = true;
  54.         int number = 0;
  55.         Scanner scan = new Scanner(System.in);
  56.         do {
  57.             try {
  58.                 number = scan.nextInt();
  59.                 if ((number > min - 1) && (number < max + 1)) {
  60.                     isCorrect = false;
  61.                 } else {
  62.                     System.out.println("Error! Enter a number. The number must be between " + min + " and " + max + "!");
  63.                 }
  64.             } catch (Exception e){
  65.                 System.out.println("Error! The number must be between " + min + " and " + max + "!" );
  66.                 scan.next();
  67.             }
  68.         } while(isCorrect);
  69.         isCorrect = true;
  70.         return number;
  71.     }
  72.  
  73.     public static void outputConsole(int[] array) {
  74.         int lengthArray = array.length;
  75.         int i = 0;
  76.         System.out.println("Pair Friendship Numbers: ");
  77.         while (i < lengthArray) {
  78.             if (array[i] == 0){
  79.                 i = i + 2;
  80.             }
  81.             else {
  82.                 System.out.println(array[i] + " | " + array[i + 1] );
  83.                 i = i + 2;
  84.             }
  85.         }
  86.     }
  87.  
  88.     public static int dividers(int number) {
  89.         int sum = 1;
  90.         for (int i = 2; i < number; i++)
  91.             if (number % i == 0) {
  92.                 sum = sum + i;
  93.             }
  94.         return sum;
  95.     }
  96.  
  97.     public static int[] searchFriendlyNumber(int range) {
  98.         int[] array = new int[range];
  99.         int a = 0;
  100.         int[] friendlyNumbers = new int[10];
  101.         for (int i = 1; i < range; i++) {
  102.             int sumDividers = dividers(i);
  103.             if (sumDividers < range && array[sumDividers] == i){
  104.                 friendlyNumbers[a] = sumDividers;
  105.                 a++;
  106.                 friendlyNumbers[a] = i;
  107.                 a++;
  108.             }
  109.             array[i] = sumDividers;
  110.         }
  111.         return friendlyNumbers;
  112.     }
  113.  
  114.     public static typeOfInput chooseInput() {
  115.         typeOfInput answer = null;
  116.         boolean isNotCorrect;
  117.  
  118.         do {
  119.             System.out.print("If you wish to fill in the matrix using the keyboard, please enter 'K', \nif you want to read an array from a file, please enter 'F': ");
  120.             char symbol = scan.next().charAt(0);
  121.             symbol = Character.toUpperCase(symbol);
  122.             isNotCorrect = false;
  123.             if ((symbol != 'K') & (symbol != 'F')) {
  124.                 System.out.println("Attention, an error occurred while input. Please try again.");
  125.                 isNotCorrect = true;
  126.             }
  127.             if (symbol == 'K') {
  128.                 answer = typeOfInput.CHOOSEKEYBOARD;
  129.             } else if (symbol == 'F') {
  130.                 answer = typeOfInput.CHOOSEFILE;
  131.             }
  132.         }
  133.         while (isNotCorrect);
  134.         return answer;
  135.     }
  136.  
  137.     public static int readRangeFromFile() {
  138.         final String INPUT_MESSAGE = "Please enter a file name from which data will be read.\nFor example,\"C:\\Users\\Eugene\\Desktop\\Name.txt\".";
  139.         boolean isInvalidInput;
  140.         int range = 0;
  141.         scan.nextLine();
  142.         do {
  143.             isInvalidInput = false;
  144.             System.out.println(INPUT_MESSAGE);
  145.             String fileName = scan.nextLine();
  146.             try {
  147.                 BufferedReader input = new BufferedReader(new FileReader(fileName));
  148.                 String text = input.readLine();
  149.                 range = Integer.parseInt(text);
  150.                 if ((range < 283) || (range > 10001)) {
  151.                     System.out.println("Error! The number in the file is not natural. Please check the file and try again.");
  152.                     isInvalidInput = true;
  153.                 }
  154.                 input.close();
  155.             } catch (FileNotFoundException e) {
  156.                 System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
  157.                 isInvalidInput = true;
  158.             } catch (IOException e) {
  159.                 System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
  160.                 isInvalidInput = true;
  161.             } catch (NumberFormatException e) {
  162.                 System.out.println("Error! The file contains invalid data. Please check the file and try again.");
  163.                 isInvalidInput = true;
  164.             }
  165.         } while (isInvalidInput);
  166.         return range;
  167.     }
  168.  
  169.     public static typeOfOutput chooseOutput() {
  170.         typeOfOutput answer = null;
  171.         boolean isNotCorrect;
  172.         do {
  173.             System.out.println("If you want to put the result in a file, please enter 'Y', \notherwise, please enter 'N':");
  174.             char symbol = scan.next().charAt(0);
  175.             symbol = Character.toUpperCase(symbol);
  176.             isNotCorrect = false;
  177.             if ((symbol != 'Y') & (symbol != 'N')) {
  178.                 System.out.println("Attention, an error occurred while entering. Please try again.");
  179.                 isNotCorrect = true;
  180.             }
  181.             if (symbol == 'Y') {
  182.                 answer = typeOfOutput.YES;
  183.             } else if (symbol == 'N') {
  184.                 answer = typeOfOutput.NO;
  185.             }
  186.         }
  187.         while (isNotCorrect);
  188.         return answer;
  189.     }
  190.  
  191.     public static void writeToFile(int[] friendlyNumbers) {
  192.         final String OUTPUT_MESSAGE = "Please enter the name of the file in which you want to write the result.\nFor example, \"C:\\Users\\Eugene\\Desktop\\Answer.txt\".";
  193.         boolean isInvalidOutput;
  194.         int i = 0;
  195.         int lengthArray = friendlyNumbers.length;
  196.         scan.nextLine();
  197.         do {
  198.             isInvalidOutput = false;
  199.             System.out.println(OUTPUT_MESSAGE);
  200.             String FileName = scan.nextLine();
  201.             try {
  202.                 BufferedWriter output = new BufferedWriter(new FileWriter(FileName));
  203.                 output.write("Pair Friendship Numbers: \n");
  204.                 while (i < lengthArray) {
  205.                     if (friendlyNumbers[i] == 0){
  206.                         i = i + 2;
  207.                     }
  208.                     else {
  209.                         output.write(friendlyNumbers[i] + " | " + friendlyNumbers[i + 1] +  "\n" );
  210.                         i = i + 2;
  211.                     }
  212.                 }
  213.                 output.close();
  214.             } catch (FileNotFoundException e) {
  215.                 System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
  216.                 isInvalidOutput = true;
  217.             } catch (IOException e) {
  218.                 System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
  219.                 isInvalidOutput = true;
  220.             }
  221.         }
  222.         while (isInvalidOutput);
  223.         System.out.println("Data recording completed successfully.");
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement