I'm very new with java, I haven't learned arrays and maps yet, so I have to create a program to convert from integer to Roman and vice-versa. My conversion from int to string works, but not string to int. It somehow does an infinite loop and I'm not too sure why. What could I change to my while loop to make it stop its infinite process ? We don't have to consider XI and such, so 4 = IIII 9 = VIIII We only take into account M, D, C, L, X, V, I We needed to create 3 constructors, one empty, one that takes a string, and one that takes a int My server class :
package jfauvelle_G10_A04;
public class RomanNumeral {
private String romanNum = "";
private int decimalNum = 0;
public RomanNumeral() {
romanNum = "";
decimalNum = 0;
}
public RomanNumeral(String r) {
decimalNum = convertRomanToInteger(r);
romanNum = r;
}
public RomanNumeral(int i) {
romanNum = convertIntegerToRoman(i);
decimalNum = i;
}
public void setRomanNumeral(String r) {
romanNum = r;
}
public String getRomanNumeral() {
return romanNum;
}
public void setDecimalNumeral(int i) {
decimalNum = i;
}
public int getDecimalNumeral() {
return decimalNum;
}
public String convertIntegerToRoman(int r) {
int roman = r;
String finalRoman = "";
while (roman >= 1000) {
finalRoman = finalRoman "M";
roman -= 1000;
} // while(r >= 1000)
while (roman >= 500) {
finalRoman = finalRoman "D";
roman -= 500;
} // while(r >= 500)
while (roman >= 100) {
finalRoman = finalRoman "C";
roman -= 100;
} // while(r >= 100)
while (roman >= 50) {
finalRoman = finalRoman "L";
roman -= 50;
} // while(r >= 50)
while (roman >= 10) {
finalRoman = finalRoman "X";
roman -= 10;
} // while(r >= 10)
while (roman >= 5) {
finalRoman = finalRoman "V";
roman -= 5;
} // while(r >= 5)
while (roman >= 1) {
finalRoman = finalRoman "I";
roman -= 1;
}
return finalRoman;
}
private int convertRomanToInteger(String n) {
String decimal = n;
int finalDecimal = 0;
for (int i = 0; i <= decimal.length(); i ) {
while (decimal.charAt(i) == 'M') {
finalDecimal = 1000;
} // while(n.charAt(i) == 'M')
while (decimal.charAt(i) == 'D') {
finalDecimal = 500;
} // while(n.charAt(i) == 'D')
while (decimal.charAt(i) == 'C') {
finalDecimal = 100;
} // while(n.charAt(i) == 'C')
while (decimal.charAt(i) == 'L') {
finalDecimal = 50;
} // while(n.charAt(i) == 'L')
while (decimal.charAt(i) == 'X') {
finalDecimal = 10;
} // while(n.charAt(i) == 'X')
while (decimal.charAt(i) == 'V') {
finalDecimal = 5;
} // while(n.charAt(i) == 'V')
while (decimal.charAt(i) == 'I') {
finalDecimal = 1;
} // while(n.charAt(i) == 'I')
} // for
return finalDecimal;
}// convertRomanToInteger()
}
Main class:
public class RomanNumeralCalculatorTestCase {
public static void main(String[] args) {
boolean working = true;
RomanNumeral case1 = new RomanNumeral();
case1.setRomanNumeral("XVI");
if (case1.getRomanNumeral() != "XVI") {
working = false;
System.err.println("ERROR: Roman numeral was not set properly. It is " case1.getRomanNumeral()
". It should be XVI");
}//if
case1.setDecimalNumeral(2004);
if (case1.getDecimalNumeral() != 2004) {
working = false;
System.err.println("ERROR: Decimal number was not set properly. It is " case1.getDecimalNumeral()
". It should be XVI");
}
RomanNumeral case2 = new RomanNumeral(1000);
String s = "M";
if(!(case2.getRomanNumeral().equals(s))) {
working = false;
System.err.println("ERROR: Decimal number was not set properly. It is " case2.getRomanNumeral()
", it should be M.");
}
RomanNumeral case3 = new RomanNumeral("M");
if(case3.getDecimalNumeral() != 1000) {
working = false;
System.err.println("ERROR: Decimal number was not set properly. It is " case3.getDecimalNumeral()
". It should be 1000");
}
if(working)
System.out.print("Congratz ! The test case work !");
}// public static void main(String[] args)
}// public class RomanNumeralCalculatorTestCase
I tried to use a small and simple array, but it remained as a string. If someone has a suggestion of a small and very simple array that I can understand, I would be really open to that solution. I just want to understand the solution.
CodePudding user response:
you probably have to change the decimalNumeral aswell when calling setRomanNumeral and vice versa otherwise the values never change
so something like
public void setDecimalNumeral(int i) {
decimalNum = i;
romanNum = convertIntegerToRoman(i);
}
also inside your while loop you never change the condition, so if you get in, you never get out, so you probably should change them to if statements
for (int i = 0; i <= decimal.length(); i ) {
if (decimal.charAt(i) == 'M') {
finalDecimal = 1000;
} // if(n.charAt(i) == 'M')