Search code examples
javaconstructorbluej

Constructor cannot be applied to given types, required no argument ; found int?


I am making a class that converts Roman numerals into arabic numbers, my code complies fine but when i try to make a client class to test it I am getting the error "constructor RomanNumerals in class RomanNumerals cannot be applied to given types; required: no arguments; found: int; reason: actual and formal argument lists differ in length".

Here is my code where i call the constructor

public class RomanConverter {

   public static void main(String[] args) {

      TextIO.putln("Enter a Roman numeral and this will change it to an");
      TextIO.putln("arabic integer.  Enter an integer.")

      while (true) {

         TextIO.putln();
         TextIO.put("? ");


         while (TextIO.peek() == ' ' || TextIO.peek() == '\t')
            TextIO.getAnyChar();
         if ( TextIO.peek() == '\n' )
            break;


         if ( Character.isDigit(TextIO.peek()) ) {
            int arab = TextIO.getlnInt();
            try {
                RomanNumerals N = new RomanNumerals(arab);
                TextIO.putln(N.toInt() + " = " + N.toString());
            }

And here is the constructor

 public void RomanNumerals(int arabic){
        num = arabic;
    }

Solution

  • You are trying to instantiate a RomanNumerals object using an overloaded constructor that you haven't defined.

    This

    public void RomanNumerals(int arabic){
        num = arabic;
    }
    

    is a method actually, not a constructor. You need to define a constructor this way:

    public RomanNumerals(int arabic){
        // Initialization
    }