# Roman numeral converter project

I have a project that is due for my Intro to Java course which requires me to translate the number range of 1 - 3999 into Roman numeral form. If the number I pick is 0 it will end my program, anything else that is below 0 or above 3999 will result in a loop until the requirements are met. I have completed my code and was wondering if there are any tips to simplify my program.

Note: I am in an intro level Java course so the use of advance techniques would be discouraged by my professor.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int num = promptUserForNumber(in);
String numeral;

if (num == 0)
{
System.out.println("Goodbye!");
}

else
{
numeral = convertNumberToNumeral(num);
System.out.println("The number " + num + " is the Roman numeral " + numeral);
}
// Fill in the body
}

// Given a Scanner as input, prompts the user to input a number between 1 and 3999.
// Checks to make sure the number is within range, and provides an error message until
// the user provides a value within range.  Returns the number input by the user to the
// calling program.
private static int promptUserForNumber(Scanner inScanner) {
System.out.print("Enter a number between 1 and 3999 (0 to quit): ");
int x = inScanner.nextInt();

if(x == 0)
{
return x;
}

while(x < 1 || x > 3999)
{
System.out.println("Your number must be between 1 and 3999");
System.out.print("Enter a number between 1 and 3999 (0 to quit): ");
x = inScanner.nextInt();

if(x == 0)
{break;}
}

return x;

// Fill in the body
}

// Given a number as input, converts the number to a String in Roman numeral format,
// following the rules in the writeup for Lab 09.  Returns the String to the calling
// program.  NOTE:  This method can possibly get long and complex.  Use the
// convertDigitToNumeral method below to break this up and make it a bit simpler to code.
private static String convertNumberToNumeral(int number) {
String str = "";
int x;
int pos = 1;

while (number != 0)
{

x = number % 10;

if (pos == 1)
{
str = convertDigitToNumeral(x, 'I', 'V', 'X') + str;
}

if (pos == 2)
{
str = convertDigitToNumeral(x, 'X', 'L', 'C') + str;
}

if (pos == 3)
{
str = convertDigitToNumeral(x, 'C', 'D', 'M') + str;
}

if(pos == 4)
{
for (int i = 0; i < x; i++)
{
str = 'M' + str;
}
}

pos++;
number = number / 10;

}

return str;
// Fill in the body
}

// Given a digit and the Roman numerals to use for the "one", "five" and "ten" positions,
// returns the appropriate Roman numeral for that digit.  For example, if the number to
// convert is 49 we would call convertDigitToNumeral twice.  The first call would be:
//     convertDigitToNumeral(9, 'I','V','X')
// and would return a value of "IX".  The second call would be:
//     convertDigitToNumeral(4, 'X','L','C')
// and would return a value of "XL".  Putting those togeter we would see that 49 would be the
// Roman numeral XLIX.
// Call this method from convertNumberToNumeral above to convert an entire number into a
// Roman numeral.
private static String convertDigitToNumeral(int digit, char one, char five, char ten) {

String str = "";
if (one == 'I')
{
for (int i = 1; i <= digit; i++)
{
if(i <= 3)
{
str = "I" + str;
}

if (i == 4)
{
str = "IV";
}

if (i == 5)
{
str = "V";
}

if (i > 5 && i <= 8)
{
str = str + "I";
}

if (i == 9)
{
str = "IX";
}
}

return str;
}

if (one == 'X')
{
for (int i = 1; i <= digit; i++)
{
if(i <= 3)
{
str = "X" + str;
}

if (i == 4)
{
str = "XL";
}

if (i == 5)
{
str = "L";
}

if (i > 5 && i <= 8)
{
str = str + "X";
}

if (i == 9)
{
str = "XC";
}
}

return str;
}

else
{
for (int i = 1; i <= digit; i++)
{
if(i <= 3)
{
str = "C" + str;
}

if (i == 4)
{
str = "CD";
}

if (i == 5)
{
str = "D";
}

if (i > 5 && i <= 8)
{
str = str + "C";
}

if (i == 9)
{
str = "CM";
}
}

return str;
}

// Fill in the body
}

• Just to clarify, this program only ever converts (at most) one line of user input into Roman numerals, correct? When reading about the "requirements [being] met" I assumed it would loop until zero, converting (multiple) inputs in the meantime. – Tersosauros Mar 12 '16 at 6:10