I am currently trying to write a program which converts integers from 0-3999 into Roman Numerals, and I have got the basis of the code correct, as when I type e.g. 6, it produces IIIIII. However, I want it to convert to the correct roman numeral, which would be VI. I feel there is something wrong with my While loop, but cannot seem to figure out why the Key in my TreeMap will always be 1, meaning the numeral will always be I. What is wrong with my code, and how do I alter it to give me the correct Roman Numeral. Any help will be appreciated.
public String generate(int number) {
// System.out.println("NUMBER: " + number);
if (number < MinNumber || number > MaxNumber) {
System.out.println("Number is out of range");
return null;
}
StringBuilder romanToString = new StringBuilder();
NavigableMap<Integer, String> romanMap = createRomanMap();
// TreeMap<Integer, String> romanMap = createRomanMap();
for (Map.Entry<Integer, String> entries : romanMap.entrySet()) {
Integer key = entries.getKey();
String value = entries.getValue();
while (number >= key) {
number -= key;
romanToString.append(value);
}
}
System.out.println("Stringbuilder: " + romanToString.toString());
return romanToString.toString();
}