Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- import java.io.*;
- public class Main {
- public static Scanner scan = new Scanner(System.in);
- public final static String CHAR_MISTAKE_MESSAGE = "Error! Enter a number.\n";
- public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! File name can not be found. Please try again.";
- public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to open this file. Please check the file and try again.";
- public enum typeOfInput {
- CHOOSEKEYBOARD, CHOOSEFILE
- }
- public enum typeOfOutput {
- YES, NO
- }
- public static void main(String[] args) {
- final String THEME_MESSAGE = "This program finds pairs of friendly numbers in a given range.";
- final String DOTTED_LINE = "--------------------------------------------------------------------\n";
- int range = 0;
- int[] friendlyNumbers = new int[0];
- System.out.print(THEME_MESSAGE);
- System.out.print(DOTTED_LINE);
- typeOfInput Input = chooseInput();
- switch (Input) {
- case CHOOSEKEYBOARD:
- System.out.print("Please enter an upper limit (from 284 to 10,000) for the friendly number search range: ");
- range = checkInputNumber(284, 10000);
- break;
- case CHOOSEFILE:
- range = readRangeFromFile();
- System.out.println("Range: " + range);
- break;
- }
- friendlyNumbers = searchFriendlyNumber(range + 1);
- outputConsole(friendlyNumbers);
- typeOfOutput Output = chooseOutput();
- switch (Output) {
- case YES:
- writeToFile(friendlyNumbers);
- break;
- case NO:
- break;
- }
- System.out.print(DOTTED_LINE);
- System.out.println("Program completed");
- }
- public static int checkInputNumber(int min, int max) {
- boolean isCorrect = true;
- int number = 0;
- Scanner scan = new Scanner(System.in);
- do {
- try {
- number = scan.nextInt();
- if ((number > min - 1) && (number < max + 1)) {
- isCorrect = false;
- } else {
- System.out.println("Error! Enter a number. The number must be between " + min + " and " + max + "!");
- }
- } catch (Exception e){
- System.out.println("Error! The number must be between " + min + " and " + max + "!" );
- scan.next();
- }
- } while(isCorrect);
- isCorrect = true;
- return number;
- }
- public static void outputConsole(int[] array) {
- int lengthArray = array.length;
- int i = 0;
- System.out.println("Pair Friendship Numbers: ");
- while (i < lengthArray) {
- if (array[i] == 0){
- i = i + 2;
- }
- else {
- System.out.println(array[i] + " | " + array[i + 1] );
- i = i + 2;
- }
- }
- }
- public static int dividers(int number) {
- int sum = 1;
- for (int i = 2; i < number; i++)
- if (number % i == 0) {
- sum = sum + i;
- }
- return sum;
- }
- public static int[] searchFriendlyNumber(int range) {
- int[] array = new int[range];
- int a = 0;
- int[] friendlyNumbers = new int[10];
- for (int i = 1; i < range; i++) {
- int sumDividers = dividers(i);
- if (sumDividers < range && array[sumDividers] == i){
- friendlyNumbers[a] = sumDividers;
- a++;
- friendlyNumbers[a] = i;
- a++;
- }
- array[i] = sumDividers;
- }
- return friendlyNumbers;
- }
- public static typeOfInput chooseInput() {
- typeOfInput answer = null;
- boolean isNotCorrect;
- do {
- 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': ");
- char symbol = scan.next().charAt(0);
- symbol = Character.toUpperCase(symbol);
- isNotCorrect = false;
- if ((symbol != 'K') & (symbol != 'F')) {
- System.out.println("Attention, an error occurred while input. Please try again.");
- isNotCorrect = true;
- }
- if (symbol == 'K') {
- answer = typeOfInput.CHOOSEKEYBOARD;
- } else if (symbol == 'F') {
- answer = typeOfInput.CHOOSEFILE;
- }
- }
- while (isNotCorrect);
- return answer;
- }
- public static int readRangeFromFile() {
- final String INPUT_MESSAGE = "Please enter a file name from which data will be read.\nFor example,\"C:\\Users\\Eugene\\Desktop\\Name.txt\".";
- boolean isInvalidInput;
- int range = 0;
- scan.nextLine();
- do {
- isInvalidInput = false;
- System.out.println(INPUT_MESSAGE);
- String fileName = scan.nextLine();
- try {
- BufferedReader input = new BufferedReader(new FileReader(fileName));
- String text = input.readLine();
- range = Integer.parseInt(text);
- if ((range < 283) || (range > 10001)) {
- System.out.println("Error! The number in the file is not natural. Please check the file and try again.");
- isInvalidInput = true;
- }
- input.close();
- } catch (FileNotFoundException e) {
- System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
- isInvalidInput = true;
- } catch (IOException e) {
- System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
- isInvalidInput = true;
- } catch (NumberFormatException e) {
- System.out.println("Error! The file contains invalid data. Please check the file and try again.");
- isInvalidInput = true;
- }
- } while (isInvalidInput);
- return range;
- }
- public static typeOfOutput chooseOutput() {
- typeOfOutput answer = null;
- boolean isNotCorrect;
- do {
- System.out.println("If you want to put the result in a file, please enter 'Y', \notherwise, please enter 'N':");
- char symbol = scan.next().charAt(0);
- symbol = Character.toUpperCase(symbol);
- isNotCorrect = false;
- if ((symbol != 'Y') & (symbol != 'N')) {
- System.out.println("Attention, an error occurred while entering. Please try again.");
- isNotCorrect = true;
- }
- if (symbol == 'Y') {
- answer = typeOfOutput.YES;
- } else if (symbol == 'N') {
- answer = typeOfOutput.NO;
- }
- }
- while (isNotCorrect);
- return answer;
- }
- public static void writeToFile(int[] friendlyNumbers) {
- 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\".";
- boolean isInvalidOutput;
- int i = 0;
- int lengthArray = friendlyNumbers.length;
- scan.nextLine();
- do {
- isInvalidOutput = false;
- System.out.println(OUTPUT_MESSAGE);
- String FileName = scan.nextLine();
- try {
- BufferedWriter output = new BufferedWriter(new FileWriter(FileName));
- output.write("Pair Friendship Numbers: \n");
- while (i < lengthArray) {
- if (friendlyNumbers[i] == 0){
- i = i + 2;
- }
- else {
- output.write(friendlyNumbers[i] + " | " + friendlyNumbers[i + 1] + "\n" );
- i = i + 2;
- }
- }
- output.close();
- } catch (FileNotFoundException e) {
- System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
- isInvalidOutput = true;
- } catch (IOException e) {
- System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
- isInvalidOutput = true;
- }
- }
- while (isInvalidOutput);
- System.out.println("Data recording completed successfully.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement