Advertisement
Guest User

romantodec

a guest
Jan 18th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. public int convertToInt(String roman) {
  2.        
  3.         //A while loop that loops for the value of length of the roman string entered subtracted by 1 to properly loop through each numeral
  4.         //A while loop is used instead of a for loop for more control within the loop allowing for a proper loop through each numeral
  5.         while(numeralIndex < roman.length()-1) {
  6.             numeralIndex++;
  7.            
  8.             //This variable stores the current numeral that the loop is iterating through in the entire roman numeral
  9.             numeral = String.valueOf(roman.charAt(numeralIndex));
  10.  
  11.             //If the loop is at the last roman numeral it will obtain the numeral before it and put it first and combine it with the current numeral after it
  12.             if(numeralIndex == roman.length()-1)
  13.                 numeral2 = ((String.valueOf(roman.charAt(numeralIndex-1)) + numeral));
  14.             else //If not, it will set numeral2 to combine the numeral first with the numeral after it
  15.                 numeral2 = (numeral + (String.valueOf(roman.charAt(numeralIndex+1))));
  16.             //This will be used later to check if it equals one of the numerals following the subtraction method i.e CM
  17.            
  18.            
  19.             //If the current numeral in the loop corresponds to one of the numerals it will add its corresponding decimal value to a total sum variable "decimal"
  20.             if(numeral.equals("M")) {
  21.                 decimal += 1000;
  22.             }
  23.             else if(numeral2.equals("CM")) {
  24.                 //The same step is done with the numerals following the subtraction method as the singular numerals
  25.                 //Although, the numeral index is moved 2x through the loop due to 2 numerals being involved
  26.                 decimal += 900;
  27.                 numeralIndex++;
  28.             }
  29.             else if(numeral.equals("D")) {
  30.                 decimal += 500;
  31.             }
  32.             else if(numeral2.equals("CD")) {
  33.                 decimal += 400;
  34.                 numeralIndex++;
  35.             }
  36.             else if(numeral.equals("C")) {
  37.                 decimal += 100;
  38.             }
  39.             else if(numeral2.equals("XC")) {
  40.                 decimal += 90;
  41.                 numeralIndex++;
  42.             }
  43.             else if(numeral.equals("L")) {
  44.                 decimal += 50;
  45.             }
  46.             else if(numeral2.equals("XL")) {
  47.                 decimal += 40;
  48.                 numeralIndex++;
  49.             }
  50.             else if(numeral.equals("X")) {
  51.                 decimal += 10;
  52.             }
  53.             else if(numeral2.equals("IX")) {
  54.                 decimal += 9;
  55.                 numeralIndex++;
  56.             }
  57.             else if(numeral.equals("V")) {
  58.                 decimal += 5;
  59.             }
  60.             else if(numeral2.equals("IV")) {
  61.                 decimal += 4;
  62.                 numeralIndex++;
  63.             }
  64.             else if(numeral.equals("I")) {
  65.                 decimal += 1;
  66.             }
  67.         }
  68.         //Once there are no more numerals to loop through it will return the converted decimal value of the roman numeral
  69.         return decimal;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement