How do I test out my program in the main method?

This will probably sound like a dumb question to many of you but I'm a new student and I am trying to learn. This is a program that takes a roman numeral input from a user and converts it to it's decimal value. I am trying to test out this program, but I don't know exactly what I have to do in my main method in order to do so. I have the other methods for the calculating but now how am I supposed to test it out? Let me show you what I have:

public class RomanNumeralConverter {  

public String getUserInput() {
    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral in uppercase: ");
    String userInput = numberInput.next();
    numberInput.close();
    return userInput;
}   

public static void romanToDecimal(String userInput) {
int decimal = 0;
int lastNumber = 0;
userInput = userInput.toUpperCase();
for (int x = userInput.length() - 1; x >= 0 ; x--) {
    char convertToDecimal = userInput.charAt(x);

    switch (convertToDecimal) {
        case 'M':
            decimal = processDecimal(1000, lastNumber, decimal);
            lastNumber = 1000;
            break;

        case 'D':
            decimal = processDecimal(500, lastNumber, decimal);
            lastNumber = 500;
            break;

        case 'C':
            decimal = processDecimal(100, lastNumber, decimal);
            lastNumber = 100;
            break;

        case 'L':
            decimal = processDecimal(50, lastNumber, decimal);
            lastNumber = 50;
            break;

        case 'X':
            decimal = processDecimal(10, lastNumber, decimal);
            lastNumber = 10;
            break;

        case 'V':
            decimal = processDecimal(5, lastNumber, decimal);
            lastNumber = 5;
            break;

        case 'I':
            decimal = processDecimal(1, lastNumber, decimal);
            lastNumber = 1;
            break;
    }
}
System.out.println(decimal);
}

public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if (lastNumber > decimal) {
    return lastDecimal - decimal;
} else {
    return lastDecimal + decimal;
}
}


public static void main(String[] args) {

romanToDecimal(getUserInput);

}
}

You could see that I tried plugging in getUserInputin to romanToDecimal but I know that I don't have those parameters in the main method, and I don't even think Java allows me to do that. But, I think this represents what I'm trying to do. Really what I want to do is:

System.out.println("The number you entered is " + userInput
System.out.println("The converted number is " + romanToDecimal

Maybe I am supposed to put this in a separate method?

Jon Skeet
people
quotationmark

There are a few changes you need:

  • If you're going to call your getUserInput method from main, you either need to make it static or create an instance of your class. I'd suggest making it a static method.
  • Currently your romanToDecimal method prints out the result - but it would be neater (in my view) if instead it returned the result, so you can print it in main
  • In romanToDecimal(getUserInput) you're trying to use getUserInput as if it's a variable, but it's a method.

After changing getUserInput to be static, and changing romanToDecimal to return a String instead of printing it, your main method could look like this:

public static void main(String[] args) {
    String input = getUserInput();
    String result = romanToDecimal(input);
    System.out.println("The number you entered is " + input);
    System.out.println("The converted number is " + result);
}

That would be fine as a program. Once you've got romanToDecimal as a method returning the result, you could also easily write unit tests for it, where the input was hard-coded into the test, and you checked the result. For example:

public void test5() {
    String result = RomanNumeralConverter.romanToDecimal("V");
    Assert.assertEquals(5, result);
}

... and lots more tests for everything you can think of. (Depending on the unit test framework you choose, you might be able to write a single piece of test code, and specify the input and expected results very compactly as parameters.)

people

See more on this question at Stackoverflow