-1

I am writing a Java program that can convert an inputted Roman Numeral into a Short, and I want it to be able to recognize and ask for input again when any letter has been inputted besides a Roman Numeral (I, V, X, L, C, D, and M).

Is there a method similar to .contains(), but with the opposite function?
Or do I have to check each individual letter with some kind of loop?

  • 2
    There is a funky "not" operator, isn't it? `if (! foo.contains(bar)) ...` – user unknown Mar 21 '18 at 01:05
  • @userunknown that will not have the desired effect; the OP wants to check for the presence of characters other than certain ones, not the absence of certain characters – O.O.Balance Mar 21 '18 at 01:11
  • @O.O.Balance: `boolean roman (String s) { for (char c: s.toCharArray()) if (! "IVXCLDM".contains(""+c)) return false; return true; }` – user unknown Mar 21 '18 at 01:38
  • @userunknown Ah, apologies, now I get it. I like this solution best, would you consider turning it into an answer? – O.O.Balance Mar 21 '18 at 01:42
  • Well, since the question showed little effort to solve the problem, I didn't want to give more than a hint. And if I had to solve it in my own code, I would go for pattern matching too. Before checking for complete numbers, I would ask for further restrictions, like 'IV, IX, IC, IM...'. – user unknown Mar 21 '18 at 01:50

3 Answers3

1

I suggest you use a regular expression to check whether the input is a Roman numeral. You can find a regular expression for this problem here. Use String#matches() to determine whether your input matches the regex.

if(!input.matches("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$")) { // input is the String the user entered
    // handle invalid input here
}
O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • `IIII` is a valid roman numeral. IV is just a short form. And IM could be used as short form for 999 too. But imho, it was only asked for valid letters, not valid numbers, so `matches ("[IVXCLDM]+")` should be sufficient. – user unknown Mar 21 '18 at 01:46
  • @userunknown Well, there's no one definition of "valid Roman numeral" :-) But you have a point, the regular expression is a bit OTT. The simpler one you suggest is sufficient. – O.O.Balance Mar 21 '18 at 01:52
1

Well of course, you need some type of filter to test against the input.

One solution could be to use a string that contains all the possible valid characters in the input and then return false if a character wasn't found in the filter.

public class HelloWorld
{
  public static boolean filter(String test, String filter) {
    for(int i = 0; i < test.length(); i++) {
        if (filter.indexOf(test.charAt(i)) == -1) {
            return false;
        }
    }
    return true;
  }
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    System.out.println(filter("XDQX", "XDQ"));
  }
}
Rabster
  • 933
  • 1
  • 7
  • 11
0

Just testing for valid characters, not for a valid sequence, can be done with contains:

boolean roman (String s) 
{
    for (char c: s.toCharArray()) 
        if (! "IVXCLDM".contains(""+c)) 
            return false; 
    return true; 
}

However, I would prefer a regular expression

boolean roman (String s) 
{
    return s.matches ("[IVXCLDM]+");
}

which means any number(+) of characters from that Set, at least one.

user unknown
  • 35,537
  • 11
  • 75
  • 121