Question
Can anyone find the errors in these codes? They're called Roman and RomanProcessor. This is just a way for us to understand errors, but I'm
Can anyone find the errors in these codes? They're called Roman and RomanProcessor. This is just a way for us to understand errors, but I'm still really new and can't figure out how to make it run properly.
FIRST (ROMAN PROCESSOR):
import java.util.Scanner; public class RomanProcessor { public static void main (String[] args) { char response; // user response to continue or not String str; // input String from user int decimal; // decimal value computed Scanner input = new Scanner (System.in); // to read user input // display purpose System.out.println ("This program reads a string, checks if it forms a"); System.out.println ("valid roman numeral and determines its decimal value."); System.out.println (); // print blank line
// see if user wants to run the program System.out.print ("Want to run? (enter y to run): "); response = input.next().charAt(0); System.out.println (); // print blank line
// process input if user wishes to continue while (response == 'y' || response == 'Y') { // prompt and read a string System.out.print ("Enter a roman numeral string: "); str = input.next(); //determine value if valid if (Roman.isValid(str)) { decimal = Roman.parseInt(str); System.out.println ("The equivalent decimal " + "value is " + decimal + "."); } else System.out.println ("Invalid roman numeral."); System.out.println (); // print blank line
// see if user wants to run the program System.out.print ("Want to run? (enter y to run): "); response = input.next().charAt(0); System.out.println (); // print blank line } // print closing remarks System.out.println ("Program has terminated."); System.out.println (); // print blank line } }
SECOND (ROMAN):
public class Roman { /** * Compute the decimal value of a roman numeral string * * @param roman a String * @return int decimal value of the roman numeral string */ public static int parseInt (String roman) { int decimalValue; // final result int len; // length of the string int index; // to step through the string char currentChar; // char currently processed int valueOfCurrentChar, valueOfPrevChar; // values of current and prev chars
// change to all uppercase and determine length roman = roman.toUpperCase(); len = roman.length();
// process first character currentChar = roman.charAt(0); valueOfCurrentChar = value (currentChar); decimalValue = valueOfCurrentChar;
// process the rest, one char at a time valueOfPrevChar = valueOfCurrentChar; for (index = 1; index <= len; index++) { currentChar = roman.charAt(index); valueOfCurrentChar = value (currentChar); if (valueOfCurrentChar <= valueOfPrevChar) decimalValue = decimalValue + valueOfCurrentChar; else decimalValue = decimalValue + valueOfCurrentChar - 2 * valueOfPrevChar; valueOfPrevChar = valueOfCurrentChar; }
//return the value computed return decimalValue; }
/** * Compute the decimal value of a single roman character * * @param romanChar a character * @return int decimal value of the roman character */ private static int value (char romanChar) { int valueOfChar; switch (romanChar) { case 'M': valueOfChar = 1000; break; case 'D': valueOfChar = 500; break; case 'C': valueOfChar = 100; break; case 'L': valueOfChar = 50; break; case 'X': valueOfChar = 20; break; case 'V': valueOfChar = 5; break; case 'I': valueOfChar = 1; break; default: valueOfChar = 0; break; } return valueOfChar; }
/** * Construct the valid roman numeral string for a decimal value * * @param decimalValue int (positive) * @return String valid roman numeral string in upper case * for decimalValue */ public static String toRoman (int decimalValue) { String roman; // result roman numeral string
// initialize to null string roman = "";
// decimalValue >= 1000 while (decimalValue >= 1000) { roman = roman + "M"; decimalValue = decimalValue - 1000; }
// decimalValue >= 100 && decimalValue < 1000 if (decimalValue >= 900) { roman = roman + "CM"; decimalValue = decimalValue - 900; } if (decimalValue >= 500) { roman = roman + "D"; decimalValue = decimalValue - 500; } if (decimalValue >= 400) { roman = roman + "CD"; decimalValue = decimalValue - 400; } while (decimalValue >= 100) { roman = roman + "C"; decimalValue = decimalValue - 100; }
// decimalValue >= 10 && decimalValue < 100 if (decimalValue >= 90) { roman = roman + "XC"; decimalValue = decimalValue - 90; } if (decimalValue >= 50) { roman = roman + "L"; decimalValue = decimalValue - 50; } if (decimalValue >= 40) { roman = roman + "XL"; decimalValue = decimalValue - 40; } while (decimalValue >= 10) { roman = roman + "X"; decimalValue = decimalValue - 10; }
// decimalValue >= 1 && decimalValue < 10 if (decimalValue >= 9) { roman = roman + "IX"; decimalValue = decimalValue - 9; } if (decimalValue >= 5) { roman = roman + "V"; decimalValue = decimalValue - 5; } if (decimalValue >= 4) { roman = roman + "IV"; decimalValue = decimalValue - 4; } while (decimalValue >= 1) { roman = roman + "I"; decimalValue = decimalValue - 1; }
// return the string formed return roman; }
/** * Verify if a string is a valid roman numeral * * @param roman a String * @return true if valid; false, otherwise */ public static boolean isValid (String roman) { return roman.toUpperCase().equals (toRoman(parseInt(roman))); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started