Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /**
- Chase Keskinyan
- 12/16/19
- Mr.Goldman
- Period 9 APCSA
- */
- public class RomanNumeral
- {
- static PrintStream Richard;
- public static String Reverse(int input)
- {
- if (input < 1 || input > 3999)
- {
- return "Invalid Roman Number Value";
- }
- String s = "";
- int[] nums = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
- String[] roman = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
- for(int i = 0; input>=1; i++)
- {
- if(input >= nums[i])
- {
- int n = input/nums[i];
- for(int j = 0; j<n; j++)
- {
- s += roman[i];
- }
- input -= nums[i]*n;
- }
- }
- return s;
- }
- public static int convert(String r)
- {
- int count = 0;
- for(int i = 0; i < r.length() - 1; i++)
- {
- if(getDigitValue(r.charAt(i)) == 0)
- {
- return 0;
- }
- else if(getDigitValue(r.charAt(i)) < getDigitValue(r.charAt(i + 1)))
- {
- count -= getDigitValue(r.charAt(i));
- }
- else
- {
- count += getDigitValue(r.charAt(i));
- }
- }
- count += getDigitValue(r.charAt(r.length() - 1));
- return count;
- }
- public static int getDigitValue(char ch)
- {
- if(ch == 'I')
- {
- return 1;
- }
- else if(ch == 'V')
- {
- return 5;
- }
- else if(ch == 'X')
- {
- return 10;
- }
- else if(ch == 'L')
- {
- return 50;
- }
- else if(ch == 'C')
- {
- return 100;
- }
- else if(ch == 'D')
- {
- return 500;
- }
- else if(ch == 'M')
- {
- return 1000;
- }
- else
- {
- return 0;
- }
- }
- public static void main(String[] args)throws Exception
- {
- try
- {
- Scanner chase = new Scanner(new FileInputStream("RomanNumeralFile.txt"));
- while(chase.hasNext())
- {
- String tmp = chase.next();
- int arabNum = convert(tmp);
- if(arabNum == 0)
- {
- System.out.println(tmp + " has a an invalid character");
- }
- else
- {
- System.out.println(tmp + " = " + arabNum);
- }
- }
- System.out.println();
- Scanner chase2 = new Scanner(new FileInputStream("RomanNumeralDigitFile.txt"));
- while(chase2.hasNext())
- {
- int arabic = chase2.nextInt();
- if(Reverse(arabic).equals("Invalid Roman Number Value"))
- {
- System.out.println(arabic + " has a an invalid character");
- }
- else
- {
- System.out.println(arabic + " = " + Reverse(arabic));
- }
- }
- }
- catch(IOException e)
- {
- System.out.println("Invalid File Name");
- }
- }
- }
- import java.io.*;
- import java.util.*;
- /**
- Chase Keskinyan
- 12/16/19
- Mr.Goldman
- Period 9 APCSA
- */
- public class RomanNumeral
- {
- static PrintStream richard;
- public static String Reverse(int input)
- {
- if (input < 1 || input > 3999)
- return "Invalid Roman Number Value";
- String s = "";
- while (input >= 1000) {
- s += "M";
- input -= 1000; }
- while (input >= 900) {
- s += "CM";
- input -= 900;
- }
- while (input >= 500) {
- s += "D";
- input -= 500;
- }
- while (input >= 400) {
- s += "CD";
- input -= 400;
- }
- while (input >= 100) {
- s += "C";
- input -= 100;
- }
- while (input >= 90) {
- s += "XC";
- input -= 90;
- }
- while (input >= 50) {
- s += "L";
- input -= 50;
- }
- while (input >= 40) {
- s += "XL";
- input -= 40;
- }
- while (input >= 10) {
- s += "X";
- input -= 10;
- }
- while (input >= 9) {
- s += "IX";
- input -= 9;
- }
- while (input >= 5) {
- s += "V";
- input -= 5;
- }
- while (input >= 4) {
- s += "IV";
- input -= 4;
- }
- while (input >= 1) {
- s += "I";
- input -= 1;
- }
- return s;
- }
- public static int convert(String r)
- {
- int count = 0;
- for(int i = 0; i < r.length() - 1; i++)
- {
- if(getDigitValue(r.charAt(i)) == 0)
- {
- return 0;
- }
- else if(getDigitValue(r.charAt(i)) < getDigitValue(r.charAt(i + 1)))
- {
- count -= getDigitValue(r.charAt(i));
- }
- else
- {
- count += getDigitValue(r.charAt(i));
- }
- }
- count += getDigitValue(r.charAt(r.length() - 1));
- return count;
- }
- public static int getDigitValue(char ch)
- {
- if(ch == 'I')
- {
- return 1;
- }
- else if(ch == 'V')
- {
- return 5;
- }
- else if(ch == 'X')
- {
- return 10;
- }
- else if(ch == 'L')
- {
- return 50;
- }
- else if(ch == 'C')
- {
- return 100;
- }
- else if(ch == 'D')
- {
- return 500;
- }
- else if(ch == 'M')
- {
- return 1000;
- }
- else
- {
- return 0;
- }
- }
- public static void main(String[] args)throws Exception
- {
- try
- {
- Scanner chase = new Scanner(new FileInputStream("RomanNumeralFile.txt"));
- while(chase.hasNext())
- {
- String tmp = chase.next();
- int arabNum = convert(tmp);
- if(arabNum == 0)
- {
- System.out.println(tmp + " has a an invalid character");
- }
- else
- {
- System.out.println(tmp + " = " + arabNum);
- }
- }
- System.out.println();
- Scanner chase2 = new Scanner(new FileInputStream("RomanNumeralDigitFile.txt"));
- while(chase2.hasNext())
- {
- int arabic = chase2.nextInt();
- if(Reverse(arabic).equals("Invalid Roman Number Value"))
- {
- System.out.println(arabic + " has a an invalid character");
- }
- else
- {
- System.out.println(arabic + " = " + Reverse(arabic));
- }
- }
- }
- catch(IOException e)
- {
- System.out.println("Invalid File Name");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment