-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanNumerals.java
74 lines (62 loc) · 2.01 KB
/
RomanNumerals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.wisetesch.global;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
// user-defined expception
class OutofBoundException extends Exception
{
OutofBoundException(String s)
{
super(s);
}
}
public class RomanNumerals {
enum Romanletters {
M(1000),CM(900),D(500),CD(400),
C(100),XC(90),L(50),XL(40),X(10),
IX(9),V(5),IV(4),I(1);
private int value;
Romanletters (int value){
this.value=value;
}
public int getValue(){
return this.value;
}
}
private static String convertToRoman(int number){
StringBuilder endValue= new StringBuilder();
Romanletters[] enumArray = Romanletters.values();
int temp=0;
while (temp<enumArray.length){
if(number>=enumArray[temp].getValue()){
endValue.append(enumArray[temp].name());
number-=enumArray[temp].getValue();
}
else {
temp++;
}
}
return endValue.toString();
}
public static void main(String[] args) {
String numberStr;
Pattern pattern = Pattern.compile(".*[0-9].*");
System.out.println("Enter the Numbers to convert to Roman Numeral and Enter 'QUIT' to exit");
try {
do {
Scanner sc = new Scanner(System.in);
numberStr = sc.nextLine();
if(pattern.matcher(numberStr).matches()) {
int numValue = Integer.valueOf(numberStr);
if((numValue>0) && (numValue<=3000))
System.out.println(" value of number : " + convertToRoman(numValue));
else
throw new OutofBoundException("VALUES TAKEN : 1 to 3000");
}
} while (!numberStr.equalsIgnoreCase("quit"));
}catch (Exception e)
{
System.out.println("Exception :" +e.getMessage());
}
}
}