Home > Net >  Changing the variable for each array in Java?
Changing the variable for each array in Java?

Time:10-20

I am creating a programme where you convert numbers to roman numbers.

Here is my code:

public static void main(String[] args) {

        System.out.println(convertDigitsToRoman(SplitNumbersIntoDigits(3567)));
    }

    public static int[] SplitNumbersIntoDigits(int numberToConvert) {
        //converting the number into digits
        int[] digit = new int[4];
        digit[0] = numberToConvert / 1000;
        digit[1] = (numberToConvert % 1000) / 100;
        digit[2] = (numberToConvert % 100) / 10;
        digit[3] = numberToConvert % 10;
        return digit;

    }

    public static String convertDigitsToRoman(int[] digit) {
        String a = "I", b = "V", c = "X";


        String allDigitInRoman = "";
        for (int i = 0; i < digit.length; i  ) {

            String digitInRoman = "";
            if (digit[i] < 4) {
                for (int j = 0; j < digit[i]; j  ) {
                    digitInRoman  = a;
                }
            } else if (digit[i] == 4) {
                digitInRoman = String.valueOf(a)   b;
            } else if (digit[i] == 5) {
                digitInRoman = String.valueOf(b);
            } else if (digit[i] > 5 && digit[i] < 9) {
                String plusA = "";
                for (int k = 5; k < digit[i]; k  ) {
                    plusA  = a;
                }
                digitInRoman = b   plusA;
            } else if (digit[i] == 9) {
                digitInRoman = String.valueOf(a)   c;
            }
            allDigitInRoman  = digitInRoman;
        }
        return allDigitInRoman;
    }

}

Now everytime when digit[i] changes I want the String a, b, c to change. How can I try to write such as:

when (digit[0]) change a,b,c to M,C,whateevr
when (digit[1]) change a,b,c to L,,whtevr,,whtevr
when (digit[2]) change a,b,c to X,whtevr,whtevr
when (digit[3]) change a,b,c to I, V, X

I know that is not java, it is English but want to know how is achieved in java.

CodePudding user response:

There is a much easier way to do this. Just like the decimal system has ones, tens, hundreds, ..., so does the Roman numeral system. Consider the following:

String[] thousands = { "", "M", "MM", "MMM" };
String[] hundreds = { "", "C", "CC", "CCC", "CD",
    "D", "DC", "DCC", "DCCC", "CM" };
String[] tens = { "", "X", "XX", "XXX", "XL", "L",
    "LX", "LXX", "LXXX", "XC" };
String[] ones = { "", "I", "II", "III", "IV", "V",
    "VI", "VII", "VIII", "IX" };
String[][] units = { ones, tens, hundreds,
    thousands };

for (int val : new int[] { 2000, 22, 45, 1986, 554,
    209, 1066, 3888 }) {
    int saveVal = val;
    String roman = "";
    int unit = 0;
    while (val > 0) {
        roman = units[unit][val % 10]   roman;
        val /= 10;
        unit  ;
    }
    System.out.printf("%-4d = %s\n", saveVal,
        roman);
}

prints

2000 = MM
22   = XXII
45   = XLV
1986 = MCMLXXXVI
554  = DLIV
209  = CCIX
1066 = MLXVI
3888 = MMMDCCCLXXXVIII
  • The above simply uses the only allowed symbols.
  • repeatedly using the remainder (%) operator retrieves the digits from right to left.
  • So the Roman numeral is built from right to left until the value is zero.

CodePudding user response:

Use a two-dimension array:

    char [][] roman = { {'I','V'}, {'X','L'},{'C','D'},{'M'}};

Note that there is no symbol for 5000, so it will be a problem if you need to convert a number that will be that large.

 int pwr = 4;
 for (int i = 0; i < digit.length;   i) {
    pwr--;
    String digitInRoman = "";
    if (digit[i] <  4) {
       for (int j = 0; j < digit[i]; j  ) {
                digitInRoman  = roman[pwr][0];

and so on.

So, for a, use roman [pwr][0], for b, use roman [pwr][1].

This changes the way you handle numbers like 9 (IX), 49 (IL), 40 (XL), and so on. To be more consistent with your current code, you might want to try something like

char [][] roman = { {'I','V','X'}, {'X','L','C'},{'C','D','M'},{'M'}};

That would allow you to use roman[pwr][2] where you have c

Off topic: You will want to add a test for zero to your code. That is, your program should properly deal with converting a number such as 1009.

  • Related