I am working on a java program that converts roman numbers. The user inputs a character string and I am needing to check to make sure that it only contains MDCLXVI and if it doesn't I will have them input the string again. I was thinking of doing a while loop but I am not sure if that is the smart way to do it or the syntax to do the check.
Here was one thought on checking with a while loop, but I have never used it before and may be building it wrong.
while (!Pattern.matches("MDCLXVI", firstRoman))
{
System.out.print("Not a valid Roman Number please try again");
firstRoman = firstRoman.toUpperCase();
}
My Code is as follows:
// Ask for user to input firstRoman Number
System.out.print("Enter your first Roman Number:");
// add the Roman Number to the String variable
firstRoman = keyboard.nextLine();
firstRoman = firstRoman.toUpperCase();
Once they enter it, I want to check to ensure that it only has the characters MDCLXVI and if it contains any other characters then I will provide them with a new Scanner Keyboard object to try again and if it fails again, then they will keep getting the option to try again until they put in a string that only contains the above characters. I just don't know where to go from here to get the while loop worked out. New to Java and I got my program to work fine when I put known good Roman Numbers in, but I want to catch incorrect entries.
Any assistance would be appreciated.
Thanks
Wally
I have put the code for my main program and my class below just in case. I have already met the teacher requirements so the project is done, I am just trying to improve on it.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myname.project.pkg6;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
*
* @author wsteadma
*/
public class mynameProject6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Used for testing
String firstRoman;
String secondRoman;
Scanner keyboard = new Scanner(System.in);
// Ask for user to input firstRoman Number
System.out.print("Enter your first Roman Number:");
// add the Roman Number to the String variable
firstRoman = keyboard.nextLine();
firstRoman = firstRoman.toUpperCase();
// Ask for user to input secondRoman Number
System.out.print("Enter your second Roman Number:");
// add the Roman Number to the String variable
secondRoman = keyboard.nextLine(); // Add the name input into the name variable
secondRoman = secondRoman.toUpperCase();
//Creating first instance of Roman Numbers class
Roman firstNumber = new Roman(firstRoman);
Roman secondNumber = new Roman(secondRoman);
System.out.println("Printout of the created roman numbers");
//Prints out the Roman Number
firstNumber.printRoman();
secondNumber.printRoman();
// Convert Roman to Decimal
firstNumber.setDecimal(firstRoman);
secondNumber.setDecimal(secondRoman);
System.out.println("Viewing the Decimal values for the two defined instances");
//Prints decimal Numbers
firstNumber.printDecimal();
secondNumber.printDecimal();
System.out.println("Adding the Roman Numbers and returning new Roman value");
Roman romanAdd;
romanAdd = firstNumber.addRoman(secondNumber);
romanAdd.printRoman();
System.out.println("Subtracting the Roman Numbers and return new Roman value");
Roman romanSub;
romanSub = firstNumber.subRoman(secondNumber);
romanSub.printRoman();
}
}
CLASS:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myname.project.pkg6;
/**
*
* @author wsteadma
*/
public class Roman {
private String romNumber;
private int decNumber;
public Roman(String rNumber) {
romNumber = rNumber;
}
public String getRoman() {
return romNumber;
}
public int getDecimal()
{
return decNumber;
}
public void printRoman() {
System.out.println("The Roman Number is " + romNumber);
}
public void printDecimal() {
System.out.println("The Decimal Number is " + decNumber);
}
// Converts a roman number string into a decimal value
public int setDecimal(String romNum) {
int charValue = 0;
char[] characters = new char[romNum.length()];
//System.out.println("romNum length is " + romNum.length());
for (int counter = 0; counter < romNum.length(); counter++) {
characters[counter] = romNum.charAt(counter);
//System.out.println(characters[counter]);
}
for (int sCounter = 0; sCounter < romNum.length(); sCounter++) {
if (characters[sCounter] == 'M' || characters[sCounter] == 'm') {
charValue += 1000;
} else if (characters[sCounter] == 'D' || characters[sCounter] == 'd') {
charValue += 500;
} else if (characters[sCounter] == 'C' || characters[sCounter] == 'c') {
charValue += 100;
} else if (characters[sCounter] == 'L' || characters[sCounter] == 'l') {
charValue += 50;
} else if (characters[sCounter] == 'X' || characters[sCounter] == 'x') {
charValue += 10;
} else if (characters[sCounter] == 'V' || characters[sCounter] == 'v') {
charValue += 5;
} else if (characters[sCounter] == 'I' || characters[sCounter] == 'i') {
charValue += 1;
}
}
decNumber = charValue;
return charValue;
}
// Converts a decomal number to a roman number
public String setDec2Roman(int decNum) {
String newRoman = "";
while (decNum >= 1000) {
newRoman += "M";
decNum -= 1000;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum >= 500) {
newRoman += "D";
decNum -= 500;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum >= 100) {
newRoman += "C";
decNum -= 100;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum >= 50) {
newRoman += "L";
decNum -= 50;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum >= 10) {
newRoman += "X";
decNum -= 10;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum >= 5) {
newRoman += "V";
decNum -= 5;
//System.out.println(decNum);
//System.out.println(newRoman);
}
while (decNum > 0) {
newRoman += "I";
decNum -= 1;
//System.out.println(decNum);
//System.out.println(newRoman);
}
return newRoman;
}
public Roman addRoman(Roman newRom) {
int totRom = newRom.setDecimal(newRom.romNumber) + decNumber;
romNumber = newRom.setDec2Roman(totRom);
return new Roman(romNumber);
}
public Roman subRoman(Roman subRom) {
int totRom = decNumber - subRom.setDecimal(subRom.romNumber);
romNumber = subRom.setDec2Roman(totRom);
return new Roman(romNumber);
}
}