Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RomanNumerals
- {
- public static void main(String []args)
- {
- //Declarations
- Scanner sc = new Scanner(System.in);
- String strRoman;
- int strValue;
- String again = "y";
- while(again.equalsIgnoreCase("y"))
- {
- strRoman = "";
- strValue = 0;
- System.out.println("Roman Numeral Converter");
- System.out.print("Please Enter a roman numeral number to convert to arabic number format: ");
- strRoman = sc.nextLine();
- if(isValid(strRoman))
- {
- strValue = getValueOfRoman(strRoman);
- System.out.println(strValue);
- }
- else
- {
- System.out.println("Your Value is not Valid.");
- }
- System.out.println("Restart Program?(y or n)");
- again = sc.nextLine();
- }
- }
- // Takes the String in and returns the actual value
- // This method will call both assignValueArray and evaluateArray
- // to do it's work.
- public static int getValueOfRoman(String strRoman)
- {
- int[] valueArray = assignValueArray(strRoman);
- int answer = evaluateArray(valueArray);
- return answer;
- }
- // Returns the value of individual 'Roman' letters
- // ie V returns 5, X returns 10 etc.
- // Hint: might be a good place for a switch
- public static int getValue(char ch)
- {
- int value = 0;
- switch(ch)
- {
- case 'I': value = 1; break;
- case 'V': value = 5; break;
- case 'X': value = 10; break;
- case 'L': value = 50; break;
- case 'C': value = 100; break;
- case 'D': value = 500; break;
- case 'M': value = 1000; break;
- }
- return value;
- }
- // Goes thru the String (for loop) and builds an int[] valueArray based on
- // the value of each letter.
- // Hint: use getValue() in your for loop
- public static int[] assignValueArray(String roman)
- {
- int [] foo = new int[roman.length()];
- for(int i = 0; i < roman.length(); i++)
- {
- foo[i] = getValue(roman.charAt(i));
- }
- return foo;
- }
- // Goes thru the valueArray and evaluates based on 1 of 2 algorithms
- public static int evaluateArray(int[]valueArray)
- {
- int answer = 0;
- for(int i = 0; i < valueArray.length; i++) //3 0-2
- {
- if(i == valueArray.length-1) //to check for last index to avoid overflow //2
- {
- answer += valueArray[i];
- }
- else if(valueArray[i+1] > valueArray[i])
- {
- answer += -(valueArray[i]);
- }
- else
- {
- answer += valueArray[i];
- }
- }
- return answer;
- }
- // Goes thru the String and checks to make sure that each char is a valid
- // roman letter/digit. If any char is not valid returns false; otherwise true.
- public static boolean isValid(String roman)
- {
- roman = roman.toUpperCase();
- for(int i=0; i<roman.length(); i++)
- {
- switch(roman.charAt(i))
- {
- case 'M':
- case 'D':
- case 'C':
- case 'L':
- case 'X':
- case 'V':
- case 'I':break;
- default: return false;
- }
- }
- return true;
- }
- }
- /*
- =====Test Cases=====
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: XIV
- 14
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: CMI
- 901
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: XXIX
- 29
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: XLIX
- 49
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: MMDCCCLX
- IV
- 2864
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: M4IVX
- Your Value is not Valid.
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: MQLXI
- Your Value is not Valid.
- Restart Program?(y or n)
- y
- Roman Numeral Converter
- Please Enter a roman numeral number to convert to arabic number format: XVIII
- 18
- Restart Program?(y or n)
- n
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement