0

I need to convert a numeric value to a roman numeral value by using different methods. Below is the code that i have created already.

public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int input = in.nextInt();
    String s = "";
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) && input != 0){
        System.out.println("Error, number must be between 1 and 3999");
         if (input == 0){
            System.out.println("Goodbye");
         }  
        }
    return input;
}
public static String convertNumberToNumeral(int input) {    
    String s = "";
    Scanner in = new Scanner(System.in);
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    }
    return s;
}

As of now there is an infinite loop and I do not know where I am going wrong. The output should be to input a numerical value between 0 and 3999 and then on the next line it should output the roman numeral. Thank you!

Michael
  • 13
  • 7

3 Answers3

1
    while (input >= 400){
    s += "CD";
    input -= 400;

} Here you need one more "}"

Shimmbo
  • 71
  • 2
  • 4
1

I don't know whether you left some code behind or not, but this code won't bring you anywhere since the main method isn't calling ohter methods.

What you want to do is call the other methods which could be done in this way:

System.out.println(convertNumberToNumeral(promptUserForNumber(in)));

Which immediately prints out the answer. When testing your code, I found another problem where your program doesn't correctly show the numbers 3999 and 2999. The problem was this:

    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    } // You forgot this curly bracket and you have 
      // to delete one curly bracket at the end of your method
    while (input >= 100){
        s += "C";
        input -= 100;
    }

Your code is also not scripted well. There is a lot of unused code. I changed it to this:

public class RomanNumbers{
static String s = "";
static Scanner scan;

public static void main (String[] args){
    scan = new Scanner(System.in);
    System.out.println(convertNumberToNumeral(promptUserForNumber()));
}

private static int promptUserForNumber() {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = scan.nextInt();

    if(input < 0 && input > 3999){
        System.out.println("Error, number must be between 1 and 3999");
    } else if(input == 0) {
        System.out.println("Goodbye");
    }
    return input;
}

public static String convertNumberToNumeral(int input) {    
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    }
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    return s;
}
}
borisjo
  • 158
  • 1
  • 16
  • I fixed my code but now in the second method, why does "scan" come up as an error, saying it cannot be resolved, and in the third method "s" cannot be resolved to a method in any of the while loops. – Michael Oct 25 '14 at 20:03
  • What error do you get for `scan`? Also `s` should be read fine since it is declared in the class and hence reachable for all methods. – borisjo Oct 26 '14 at 22:20
0
public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int i = promptUserForNumber(in);
    if (i != -1) {
        //convertNumberToNumeral
    } else {
        //invalid no is entered
    }
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) || input == 0) {
        System.out.println("Error, number must be between 1 and 3999");
         return -1;
    }
    return input;
}

Also, you are missing one } as already pointed out by Jimmy.

Tirath
  • 2,294
  • 18
  • 27