tcrowson
tcrowson

Reputation: 85

Converting Roman numeral to number logical error java

So I have a homework assignment where I have to be able to take a Roman numeral and convert vice versa. I understand how to go from number to letter, but letters to numbers is messing me up. I need help because I wrote my program and Roman numerals to number kind of works. For instance, if I try to do, XCIX == 99, i get 199. But if I do the roman numeral for 2014, that works. Also if I only input a single letter I get 0. I just need help understanding what I need to do to fix the problem.

import java.util.HashMap;
import java.util.Scanner;

public class Apweek2 {

public static void main(String[] args) {
    Scanner userinput = new Scanner(System.in);
    System.out.print("enter a number to convert to roman numerals: ");
    int input = userinput.nextInt();

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD",
            "D", "CM", "M" };
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
            1000 };

    System.out.print("enter a roman numeral: ");
    String roman_numeral = userinput.next();

    int sum = 0;
    String two_spot = null;
    String last_value = null;
    for (int i = 1, j = 0; j < roman_numeral.length()
            && i < roman_numeral.length(); i++, j++) {

        last_value = roman_numeral.substring(j, i);

        char roman_noodles = roman_numeral.charAt(i);
        char raman_noodles = roman_numeral.charAt(j);

        String roman_values = Character.toString(roman_noodles);

        two_spot = last_value + roman_values;

        if (two_spot.contains(rv[1])) {
            sum = sum + values_for_rv[1];
        }
        if (two_spot.contains(rv[3])) {
            sum = sum + values_for_rv[3];
        }
        if (two_spot.contains(rv[5])) {
            sum = sum + values_for_rv[5];
        }
        if (two_spot.contains(rv[7])) {
            sum = sum + values_for_rv[7];
        }
        if (two_spot.contains(rv[9])) {
            sum = sum + values_for_rv[9];
        }
        if (two_spot.contains(rv[11])) {
            sum = sum + values_for_rv[11];
        }

        if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3]))
                && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7]))
                && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) {

            if (raman_noodles == 'I') {
                sum = sum + 1;
            }
            if (raman_noodles == 'V') {
                sum = sum + 5;
            }
            if (raman_noodles == 'X') {
                sum = sum + 10;
            }
            if (raman_noodles == 'L') {
                sum = sum + 50;
            }
            if (raman_noodles == 'C') {
                sum = sum + 100;
            }
            if (raman_noodles == 'D') {
                sum = sum + 500;
            }
            if (raman_noodles == 'M') {
                sum = sum + 1000;
            }

        }

    }
    System.out.println("converted roman numeral is: " + sum);

    String inputconversion = inputtoroman(input);

    System.out.print("Converted number is: " + inputconversion);
}

public static String inputtoroman(int x) {

    String s1 = "";
    String s2 = "";
    String s3 = "";
    String s4 = "";
    String s5 = "";
    String s6 = "";
    String s7 = "";
    String s8 = "";
    String s9 = "";
    String s10 = "";
    String s11 = "";
    String s12 = "";
    String s13 = "";

    while (x >= 1000) {
        s1 += "M";
        x -= 1000;
    }
    while (x >= 900) {
        s2 += "CM";
        x -= 900;
    }
    while (x >= 500) {
        s3 += "D";
        x -= 500;
    }
    while (x >= 400) {
        s4 += "CD";
        x -= 400;
    }
    while (x >= 100) {
        s5 += "C";
        x -= 100;
    }
    while (x >= 90) {
        s6 += "XC";
        x -= 90;
    }
    while (x >= 50) {
        s7 += "L";
        x -= 50;
    }
    while (x >= 40) {
        s8 += "XL";
        x -= 40;
    }
    while (x >= 10) {
        s9 += "X";
        x -= 10;
    }
    while (x >= 9) {
        s10 += "IX";
        x -= 9;
    }
    while (x >= 5) {
        s11 += "V";
        x -= 5;
    }
    while (x >= 4) {
        s12 += "IV";
        x -= 4;
    }
    while (x >= 1) {
        s13 += "I";
        x -= 1;
    }
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10
            + s11 + s12 + s13;
    return combined;

}
}

Upvotes: 1

Views: 733

Answers (2)

tcrowson
tcrowson

Reputation: 85

So i finally figured out my problems in my code, i had to increment the indexes twice for the entire two_spot when i found a match too one of the roman numerals that had 2 values like IV. Then i also had to find the last value and calculate it because the for loop only did evens kind of correctly. Then i also had to increment the value again twice if it didn't find a match to value like IV. Hence I move through the string in pairs of two and calculate in pairs of two unless the string has an odd length. Which it calculates the last value in the string and proceeds all the even ones. Finally, if anyone ever needs help with converting roman numerals either way, and are a beginner to java, this should help.

import java.util.Scanner;

public class Apweek2 {

public static void main(String[] args) {
    Scanner userinput = new Scanner(System.in);
    System.out.print("enter a number to convert to roman numerals: ");
    int input = userinput.nextInt();

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD",
            "D", "CM", "M" };
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
            1000 };

    System.out.print("enter a roman numeral: ");
    String roman_numeral = userinput.next().toUpperCase();


    int sum = 0;

    if (roman_numeral.length() % 2 == 0) {

    } else {
        char odd_value = roman_numeral.charAt(roman_numeral.length() - 1);
        if (odd_value == 'I') {
            sum = sum + 1;
        }
        if (odd_value == 'V') {
            sum = sum + 5;
        }
        if (odd_value == 'X') {
            sum = sum + 10;
        }
        if (odd_value == 'L') {
            sum = sum + 50;
        }
        if (odd_value == 'C') {
            sum = sum + 100;
        }
        if (odd_value == 'D') {
            sum = sum + 500;
        }
        if (odd_value == 'M') {
            sum = sum + 1000;
        }
    }

    String two_spot = null;
    String last_value = null;
    for (int i = 1, j = 0; j < roman_numeral.length()
            && i < roman_numeral.length(); i++, j++) {

        last_value = roman_numeral.substring(j, i);

        char roman_noodles = roman_numeral.charAt(i);

        String roman_values = Character.toString(roman_noodles);

        two_spot = last_value + roman_values;

        if (two_spot.contains(rv[1])) {
            sum = sum + values_for_rv[1];
            i++;
            j++;
        }
        if (two_spot.contains(rv[3])) {
            sum = sum + values_for_rv[3];
            i++;
            j++;
        }
        if (two_spot.contains(rv[5])) {
            sum = sum + values_for_rv[5];
            i++;
            j++;
        }
        if (two_spot.contains(rv[7])) {
            sum = sum + values_for_rv[7];
            i++;
            j++;
        }
        if (two_spot.contains(rv[9])) {
            sum = sum + values_for_rv[9];
            i++;
            j++;
        }
        if (two_spot.contains(rv[11])) {
            sum = sum + values_for_rv[11];
            i++;
            j++;
        }

        if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3]))
                && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7]))
                && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) {

            for (int k = 0; k < two_spot.length(); k++) {

                char raman_noodles = two_spot.charAt(k);

                if (raman_noodles == 'I') {
                    sum = sum + 1;
                }
                if (raman_noodles == 'V') {
                    sum = sum + 5;
                }
                if (raman_noodles == 'X') {
                    sum = sum + 10;
                }
                if (raman_noodles == 'L') {
                    sum = sum + 50;
                }
                if (raman_noodles == 'C') {
                    sum = sum + 100;
                }
                if (raman_noodles == 'D') {
                    sum = sum + 500;
                }
                if (raman_noodles == 'M') {
                    sum = sum + 1000;
                }

            }
            i++;
            j++;
        }

    }
    System.out.println("Converted roman numeral to number is: "+sum);

    String inputconversion = inputtoroman(input);

    System.out.print("Converted number is: " + inputconversion);
}

public static String inputtoroman(int x) {

    String s1 = "";
    String s2 = "";
    String s3 = "";
    String s4 = "";
    String s5 = "";
    String s6 = "";
    String s7 = "";
    String s8 = "";
    String s9 = "";
    String s10 = "";
    String s11 = "";
    String s12 = "";
    String s13 = "";

    while (x >= 1000) {
        s1 += "M";
        x -= 1000;
    }
    while (x >= 900) {
        s2 += "CM";
        x -= 900;
    }
    while (x >= 500) {
        s3 += "D";
        x -= 500;
    }
    while (x >= 400) {
        s4 += "CD";
        x -= 400;
    }
    while (x >= 100) {
        s5 += "C";
        x -= 100;
    }
    while (x >= 90) {
        s6 += "XC";
        x -= 90;
    }
    while (x >= 50) {
        s7 += "L";
        x -= 50;
    }
    while (x >= 40) {
        s8 += "XL";
        x -= 40;
    }
    while (x >= 10) {
        s9 += "X";
        x -= 10;
    }
    while (x >= 9) {
        s10 += "IX";
        x -= 9;
    }
    while (x >= 5) {
        s11 += "V";
        x -= 5;
    }
    while (x >= 4) {
        s12 += "IV";
        x -= 4;
    }
    while (x >= 1) {
        s13 += "I";
        x -= 1;
    }
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10
            + s11 + s12 + s13;
    return combined;

  }

}

Upvotes: 0

Andrew Alderson
Andrew Alderson

Reputation: 903

You start i at 1, so when you input only 1 number, it won't enter the loop at all. Should be changed to compare with the full length of roman_numeral.

for (int i = 1, j = 0; j < roman_numeral.length()
        && i <= roman_numeral.length(); i++, j++) {

This will probably also affect how it loops through, but I can't run it myself at the moment.

edit: Actually, looking at this again, I can already see it's going to crash on char roman_noodles = roman_numeral.charAt(i);

You'll need to add some additional logic if you want it to check just 1 character.

Upvotes: 2

Related Questions