Search code examples
javabluej

Java.lang.String cannot be converted to int


In my program that turns roman numerals into arabic numbers I have run across the error

incompatible types: java.lang.String cannot be converted into int

Here is my code

if ( Character.isDigit(TextIO.peek()) ) {
    int arabic = TextIO.getlnInt();
    try {
        RomanNumerals N = new RomanNumerals(arabic);
        TextIO.putln(N.toInt() + " = " + N.toString());
    }
    catch (NumberFormatException e) {
        System.out.println("Invalid input.");
        System.out.println(e.getMessage());
    }
}
else {
    String roman = TextIO.getln();
    try {
        RomanNumerals N = new RomanNumerals(roman);
        System.out.println(N.toString() + " = " + N.toInt());
    }
    catch (NumberFormatException e) {
        System.out.println("Invalid input.");
        System.out.println(e.getMessage());
    }
}

I am using BlueJ and the error is being highlighted over "(roman)"


Solution

  • Guesswork here... but probably your class RomanNumerals does not have a constructor taking a string as an argument like

    public RomanNumerals(String r) {
    

    Thats why calling it that way:

    RomanNumerals N = new RomanNumerals(roman);
    

    Is not permitted.