ChaseKeskinyan

RomanNum

Jan 3rd, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. /**
  4. Chase Keskinyan
  5. 12/16/19
  6. Mr.Goldman
  7. Period 9 APCSA
  8. */
  9. public class RomanNumeral
  10. {
  11. static PrintStream Richard;
  12. public static String Reverse(int input)
  13. {
  14. if (input < 1 || input > 3999)
  15. {
  16. return "Invalid Roman Number Value";
  17. }
  18.  
  19. String s = "";
  20. int[] nums = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
  21. String[] roman = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  22.  
  23. for(int i = 0; input>=1; i++)
  24. {
  25. if(input >= nums[i])
  26. {
  27. int n = input/nums[i];
  28.  
  29. for(int j = 0; j<n; j++)
  30. {
  31. s += roman[i];
  32. }
  33.  
  34. input -= nums[i]*n;
  35. }
  36. }
  37.  
  38. return s;
  39. }
  40.  
  41. public static int convert(String r)
  42. {
  43. int count = 0;
  44. for(int i = 0; i < r.length() - 1; i++)
  45. {
  46. if(getDigitValue(r.charAt(i)) == 0)
  47. {
  48. return 0;
  49. }
  50. else if(getDigitValue(r.charAt(i)) < getDigitValue(r.charAt(i + 1)))
  51. {
  52. count -= getDigitValue(r.charAt(i));
  53. }
  54. else
  55. {
  56. count += getDigitValue(r.charAt(i));
  57. }
  58. }
  59. count += getDigitValue(r.charAt(r.length() - 1));
  60. return count;
  61. }
  62.  
  63. public static int getDigitValue(char ch)
  64. {
  65. if(ch == 'I')
  66. {
  67. return 1;
  68. }
  69.  
  70. else if(ch == 'V')
  71. {
  72. return 5;
  73. }
  74.  
  75. else if(ch == 'X')
  76. {
  77. return 10;
  78. }
  79.  
  80. else if(ch == 'L')
  81. {
  82. return 50;
  83. }
  84.  
  85. else if(ch == 'C')
  86. {
  87. return 100;
  88. }
  89.  
  90. else if(ch == 'D')
  91. {
  92. return 500;
  93. }
  94.  
  95. else if(ch == 'M')
  96. {
  97. return 1000;
  98. }
  99.  
  100. else
  101. {
  102. return 0;
  103. }
  104. }
  105.  
  106. public static void main(String[] args)throws Exception
  107. {
  108. try
  109. {
  110. Scanner chase = new Scanner(new FileInputStream("RomanNumeralFile.txt"));
  111. while(chase.hasNext())
  112. {
  113. String tmp = chase.next();
  114. int arabNum = convert(tmp);
  115. if(arabNum == 0)
  116. {
  117. System.out.println(tmp + " has a an invalid character");
  118. }
  119. else
  120. {
  121. System.out.println(tmp + " = " + arabNum);
  122. }
  123. }
  124. System.out.println();
  125. Scanner chase2 = new Scanner(new FileInputStream("RomanNumeralDigitFile.txt"));
  126. while(chase2.hasNext())
  127. {
  128. int arabic = chase2.nextInt();
  129. if(Reverse(arabic).equals("Invalid Roman Number Value"))
  130. {
  131. System.out.println(arabic + " has a an invalid character");
  132. }
  133. else
  134. {
  135. System.out.println(arabic + " = " + Reverse(arabic));
  136. }
  137. }
  138. }
  139.  
  140. catch(IOException e)
  141. {
  142. System.out.println("Invalid File Name");
  143. }
  144. }
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. import java.io.*;
  161. import java.util.*;
  162. /**
  163. Chase Keskinyan
  164. 12/16/19
  165. Mr.Goldman
  166. Period 9 APCSA
  167. */
  168. public class RomanNumeral
  169. {
  170. static PrintStream richard;
  171.  
  172. public static String Reverse(int input)
  173. {
  174. if (input < 1 || input > 3999)
  175. return "Invalid Roman Number Value";
  176. String s = "";
  177. while (input >= 1000) {
  178. s += "M";
  179. input -= 1000; }
  180. while (input >= 900) {
  181. s += "CM";
  182. input -= 900;
  183. }
  184. while (input >= 500) {
  185. s += "D";
  186. input -= 500;
  187. }
  188. while (input >= 400) {
  189. s += "CD";
  190. input -= 400;
  191. }
  192. while (input >= 100) {
  193. s += "C";
  194. input -= 100;
  195. }
  196. while (input >= 90) {
  197. s += "XC";
  198. input -= 90;
  199. }
  200. while (input >= 50) {
  201. s += "L";
  202. input -= 50;
  203. }
  204. while (input >= 40) {
  205. s += "XL";
  206. input -= 40;
  207. }
  208. while (input >= 10) {
  209. s += "X";
  210. input -= 10;
  211. }
  212. while (input >= 9) {
  213. s += "IX";
  214. input -= 9;
  215. }
  216. while (input >= 5) {
  217. s += "V";
  218. input -= 5;
  219. }
  220. while (input >= 4) {
  221. s += "IV";
  222. input -= 4;
  223. }
  224. while (input >= 1) {
  225. s += "I";
  226. input -= 1;
  227. }
  228. return s;
  229. }
  230.  
  231. public static int convert(String r)
  232. {
  233. int count = 0;
  234. for(int i = 0; i < r.length() - 1; i++)
  235. {
  236. if(getDigitValue(r.charAt(i)) == 0)
  237. {
  238. return 0;
  239. }
  240. else if(getDigitValue(r.charAt(i)) < getDigitValue(r.charAt(i + 1)))
  241. {
  242. count -= getDigitValue(r.charAt(i));
  243. }
  244. else
  245. {
  246. count += getDigitValue(r.charAt(i));
  247. }
  248. }
  249. count += getDigitValue(r.charAt(r.length() - 1));
  250. return count;
  251. }
  252.  
  253. public static int getDigitValue(char ch)
  254. {
  255. if(ch == 'I')
  256. {
  257. return 1;
  258. }
  259.  
  260. else if(ch == 'V')
  261. {
  262. return 5;
  263. }
  264.  
  265. else if(ch == 'X')
  266. {
  267. return 10;
  268. }
  269.  
  270. else if(ch == 'L')
  271. {
  272. return 50;
  273. }
  274.  
  275. else if(ch == 'C')
  276. {
  277. return 100;
  278. }
  279.  
  280. else if(ch == 'D')
  281. {
  282. return 500;
  283. }
  284.  
  285. else if(ch == 'M')
  286. {
  287. return 1000;
  288. }
  289.  
  290. else
  291. {
  292. return 0;
  293. }
  294. }
  295.  
  296. public static void main(String[] args)throws Exception
  297. {
  298. try
  299. {
  300. Scanner chase = new Scanner(new FileInputStream("RomanNumeralFile.txt"));
  301. while(chase.hasNext())
  302. {
  303. String tmp = chase.next();
  304. int arabNum = convert(tmp);
  305. if(arabNum == 0)
  306. {
  307. System.out.println(tmp + " has a an invalid character");
  308. }
  309. else
  310. {
  311. System.out.println(tmp + " = " + arabNum);
  312. }
  313. }
  314. System.out.println();
  315. Scanner chase2 = new Scanner(new FileInputStream("RomanNumeralDigitFile.txt"));
  316. while(chase2.hasNext())
  317. {
  318. int arabic = chase2.nextInt();
  319. if(Reverse(arabic).equals("Invalid Roman Number Value"))
  320. {
  321. System.out.println(arabic + " has a an invalid character");
  322. }
  323. else
  324. {
  325. System.out.println(arabic + " = " + Reverse(arabic));
  326. }
  327. }
  328. }
  329.  
  330. catch(IOException e)
  331. {
  332. System.out.println("Invalid File Name");
  333. }
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment