Lenotre Lenotre - 1 month ago 6
Java Question

Looping multiple scanner inputs that work together until a specific input

I am new to java, and I am doing this assignment for a romanCalculator.
I am currently done with the calculation part but i am having an issue with some of the rules requested.

I want 3 type of scanner inputs to work.


  1. (possible Roman number) (operator)
  2. (operator) (possible Roman number)

  3. .



The first will take a roman numeral input and use any operator on another roman numeral input
Roman numerals are [MMCDXLVI].

I was able to create the program which takes 2 roman numerals and u calculate them depending on the operator input, but I wasn't able to have the second rule work.

It means if there's a result from rule #1, and i type (operator) (possible Roman number), it will calculate the last Roman number( the result) with the new input.

For example:


X + I (input)

XI (output)

* II (input)

XXII (output)



So how do i make that work, how to have 2 viable inputs only and the second input uses the result of the first input.

This is all what i could do with my knowledge:

Scanner in = new Scanner(System.in);
String input = "";

while(!(input = in.nextLine()).equals("."))
{
String[] userInput = input.split("\\s+");



if(userInput.length == 3)


{
String firstRoman = userInput[0];
String operator = userInput[1];
String secondRoman = userInput[2];

int romanConvertedNumber = (compute(romanToNumber(firstRoman), operator, romanToNumber(secondRoman)));



if(firstRoman.matches("[MCDXLVImcdxlvi]+") && operator.matches("\\+|\\-|\\*|\\/") && secondRoman.matches("[MCDXLVImcdxlvi]+"))
{

IntegerToRomanNumeral(romanConvertedNumber); //method to convert number to roman

}


}

else{
System.out.println("Error: Wrong input");
}
}

}


It works for the first case only like said and terminates when i write (.), I don't how to apply to the result i get to (operator) (possible roman Number) like in the example, I'd appreciate any help. Thanks!

Answer

I think it should be more like this, i did a few changes..

  1. check if valid input format (using matches) should be before the compute call

  2. in case its not valid input, you should also print an error message (even if length of input is 3 strings)

  3. add new variable lastResult

  4. actually print the result :-)

hope its good enough

Scanner in = new Scanner(System.in);
String input = "";
String lastResult = "";

while(!(input = in.nextLine()).equals("."))
{
    String[] userInput = input.split("\\s+");

    if(userInput.length == 3)
    {
        String firstRoman = userInput[0];
        String operator = userInput[1];
        String secondRoman = userInput[2];

        if(firstRoman.matches("[MCDXLVImcdxlvi]+") && operator.matches("\\+|\\-|\\*|\\/") && secondRoman.matches("[MCDXLVImcdxlvi]+"))
        {
            int romanConvertedNumber = (compute(romanToNumber(firstRoman), operator, romanToNumber(secondRoman)));
            lastResult = IntegerToRomanNumeral(romanConvertedNumber); //method to convert number to roman
            System.out.println(lastResult);
        }
        else{
            System.out.println("Error: Wrong input");
        }

    }
    /* check if */
    else if( ( userInput.length == 2 ) & ( ! lastResult.equals("") ) ){ 
        String firstRoman = lastResult;
        String operator = userInput[0];
        String secondRoman = userInput[1];

        if(firstRoman.matches("[MCDXLVImcdxlvi]+") && operator.matches("\\+|\\-|\\*|\\/") && secondRoman.matches("[MCDXLVImcdxlvi]+"))
        {
            int romanConvertedNumber = (compute(romanToNumber(firstRoman), operator, romanToNumber(secondRoman)));
            lastResult = IntegerToRomanNumeral(romanConvertedNumber); //method to convert number to roman
            System.out.println(lastResult);
        }
        else{
            System.out.println("Error: Wrong input");
        }
    }
    else{
        System.out.println("Error: Wrong input");
    }
}