0

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.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
Skyzreal
  • 21
  • 7
  • 1
    To diagnose infinite loops, run your program in a debugger, then suspend execution and inspect the state of the program (and optionally, step through an iteration of the loop to see why it doesn't make progress). – meriton Dec 05 '22 at 17:02
  • 1
    `case1.getRomanNumeral() != "XVI"` won't work: Please have a look at [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – QBrute Dec 05 '22 at 17:02
  • Thanks, I just changed my case1 to .equals, I completely forgot. And I really tried to run the debugger, but Eclipse is giving me a lot of issues. I keep having the "Editor does not contain a main type" message, even though there is one – Skyzreal Dec 05 '22 at 17:28
  • There is a logical progression of Roman to Arabic and Arabic to Roman. Check out [this](https://stackoverflow.com/a/66038188/1552534) as an example. – WJS Dec 05 '22 at 17:29
  • You could use an `enum` for your Roman numerals – g00se Dec 05 '22 at 17:53

1 Answers1

0

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')
  • Thank you ! I changed my while for if and it worked. I keep forgetting stupid things, including that while, do.. while and for needs updater statement always. Thanks again ! – Skyzreal Dec 05 '22 at 17:33
  • another thing i found is in the `convertRomanToInteger(String n)` Method the condition in the for-loop should be `i < decimal.length()` instead of `i <= decimal.length()` since indexing starts at 0 and ends at `length()-1` – Ivo Heberle Dec 05 '22 at 17:51
  • Yes, I actually resolved that issue by adding -1, but your solution would've worked as well. Thanks again ! :D – Skyzreal Dec 06 '22 at 19:29