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 converts the number to the Roman numeral system");
- int source = chooseSourceOfInput();
- String sourceNumber = inputLine(source);
- String romanNumber = convertToRoman(sourceNumber);
- outputResult(source, romanNumber, sourceNumber);
- }
- public static int chooseSourceOfInput(){
- 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 String inputLine(int source) throws FileNotFoundException {
- String line = "";
- switch (source){
- case 1:
- System.out.println("Enter the absolute link to the input file");
- String pathInput = inputPath();
- line = inputLineFromFile(pathInput);
- break;
- case 2:
- line = inputLineFromConsole();
- break;
- }
- return line;
- }
- 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 String inputLineFromFile(String path) throws FileNotFoundException {
- File inputFile = new File(path);
- Scanner scan = new Scanner(inputFile);
- String line;
- if (inputFile.length() != 0){
- line = scan.nextLine();
- }else{
- System.out.println("The line is missing from your file!\nEnter line from console ");
- line = inputLineFromConsole();
- }
- boolean isIncorrect = checkLine(line);
- if (isIncorrect) {
- line = inputLineFromConsole();
- }
- return line;
- }
- public static boolean checkLine (String line) {
- boolean flag = false;
- int number = 0;
- if (line.length() == 0){
- System.out.println("You entered an empty line!\nRepeat enter");
- flag = true;
- }
- if (!flag) {
- try {
- number = Integer.parseInt(line);
- } catch (NumberFormatException e) {
- System.out.println("This line cannot be represented as an integer!\nEnter line from console ");
- flag = true;
- }
- }
- if ((number < 1 || number > 2000) && !flag) {
- System.out.println("The number must be in the range from 1 to 2000\nEnter line from console ");
- flag = true;
- }
- return flag;
- }
- public static String inputLineFromConsole() {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String line;
- System.out.println("Enter the line");
- do {
- isIncorrect = false;
- line = scan.nextLine();
- isIncorrect = checkLine(line);
- }while (isIncorrect);
- return line;
- }
- public static String convertToRoman(String sourceNumber){
- String[] unitsOfRoman = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
- String[] tensOfRoman = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
- String[] hundredsOfRoman = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
- String[] thousandsOfRoman = {"","M","MM"};
- int j = 0;
- int[] digits = new int[4];
- for (int i = 4 - sourceNumber.length(); i < 4; i++) {
- digits[i] = Integer.parseInt(String.valueOf(sourceNumber.charAt(j)));
- j++;
- }
- String romanNumber = thousandsOfRoman[digits[0]] + hundredsOfRoman[digits[1]] + tensOfRoman[digits[2]] + unitsOfRoman[digits[3]];
- return romanNumber;
- }
- public static void outputResult(int source, String romanNumber, String sourceNumber ) throws IOException {
- if (source == 1) {
- System.out.println("\nEnter the absolute link to the output file");
- String pathOutput = inputPath();
- outputToFile(pathOutput, romanNumber, sourceNumber);
- }else {
- outputToConsole(romanNumber, sourceNumber);
- }
- }
- public static void outputToFile(String path, String romanNumber, String sourceNumber) throws IOException {
- FileWriter writer = new FileWriter(path);
- writer.write(sourceNumber + " = " + romanNumber);
- writer.close();
- System.out.println("The data is successfully recorded in the file");
- }
- public static void outputToConsole(String romanNumber, String sourceNumber){
- System.out.println(sourceNumber + " = " + romanNumber);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement