NEPat10
NEPat10

Reputation: 159

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?

Upvotes: 1

Views: 359

Answers (6)

chip
chip

Reputation: 1

If you put the userInput method into your main method then pass userInput to romanToDecimal(getUserInput); in the main method it will work here is the code for the main method

public static void main(String[] args) {
Scanner numberInput = new Scanner (System.in);

System.out.print("Enter a roman numeral in uppercase: ");
String userInput = numberInput.next();
numberInput.close();
romanToDecimal(userInput);`

  }
}

Upvotes: 0

Dima
Dima

Reputation: 40500

getUserInput is a function name. getUserInput() is a call to the function, called getUserInput, passing no arguments to it (empty parenthesis). That's what you want: call that function, and use the string it returns. romanToDecimal(getUserInput()); in your main function should work. You can also assign it to a variable first, so that you could print it out before calling romanToDecimal, and also, make romatToDecimal return a String rather than just printing it out. Then you could do something like this:

public static void main(String argv[]) {
    String input = getUserInput();
    String output = romanToDecimal(input);
    System.out.ptinln("You enetered: " + input + "\nThe converted number is: " + output);
}

Oh, and as someone has pointed out, you need to make both methods you are calling static.

Upvotes: 0

Shahzad
Shahzad

Reputation: 2063

try this:

public static void main(String[] args) {
    RomanNumeralConverter rnc = new RomanNumeralConverter();
    String userInput = rnc.getUserInput(); // get user input
    System.out.println(userInput); // print user input
    Tests.romanToDecimal(userInput); // call the romanToDecimal static method to convert and print converted decimal
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500055

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.)

Upvotes: 2

Drejc
Drejc

Reputation: 14286

Just write unit tests testing out the romanToDecimal method with various inputs. In Java JUnit is the most popular framework for doing this.

The test would look something like this:

@Test
public void testMyMethod() {

   assertEquals("expected result", romanToDecimal("input"));
}

PS: In order to do this successfully you will need to return a String in your romanToDecimal method!

Upvotes: 0

namsnath
namsnath

Reputation: 158

Do this in the main method:

String input = getUserInput();
romanToDecimal(input);

This should get the code working like it should.

Upvotes: 0

Related Questions