0

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

1
  • The constructor for RomanNumerals must expect an int, and you're giving it a String. You need to convert it into an int. Commented Jan 9, 2016 at 19:35

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm looking at TextIO.putln(N.toInt() + " = " + N.toString()); and imagining that N.toInt() returns an int and the compiler is confused when you try adding " = " to it.

Try TextIO.putln(Integer(N.toInt()).toString() + " = " + N.toString());.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.