The constructor in the class cannot be applied to the specified types.

I have the following code using arrays to find some prime numbers. However, when trying to compile my custom PalindromeArrayUser class, he says: "The constructor in the class cannot be applied to the given types"

required: int. found: no arguments. reason: lists of actual and formal arguments vary in length.

However, I passed the constructor an int value (just as it was developed in my project). I don’t quite understand where this problem comes from. Thank you

Here are my two classes

 public class PalindromeArray 
 {

int arrLength;

public PalindromeArray(int InputValue) 
{
    arrLength = InputValue;
}


int arr[] = new int[arrLength];
boolean check[] = new boolean [arrLength];


public void InitializeArray()  
{

    for (int k = 2; k < arr.length; k++)
    {
        arr[k] = k;
        check[k] = true;

    }   
}

public void primeCheck()  
{

    for (int i = 2; i < Math.sqrt(arr.length - 1); i++ )
    {
        if (check[i] == true)
        {
        for (int j = 2; j < arr.length; j++)
          {
            if (j % i == 0)
                {
                     check[j] = false;
                     check[i] = true;
                }
          }
        }   

    }   
}

public void PrintArray() 
{
    for (int k = 2; k < arr.length; k++)
    {
        if ((!check[k]) == false)
            System.out.println(arr[k]);

    }
}

   }

And this is my user class from which the problem arises. The class above compiles fine.

 import java.io.*;

 public class PalindromeArrayUser extends PalindromeArray
 {
public static void main(String argv[]) throws IOException 
{
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter the upper bound.");

    String line = input.readLine();

    int InputUser = Integer.parseInt(line);
                                     // this is where I pass the same int type as I  
                                                  // constructed it
    PalindromeArray palindrome = new PalindromeArray(InputUser);
    palindrome.InitializeArray();
    palindrome.primeCheck();
    palindrome.PrintArray();


}

 }
+4
source share
3 answers

, . , no-arg , .

:

class Parent {
  int i;
  public Parent(int i) {
    this.i=i;
  }
}

class Child extends Parent {
  int j;
  public Child(int i, int j) {
    super(i);
    this.j=j;
  }
  public Child(int j) {
    // here a call to super() is made, but since there is no no-arg constructor
    // for class Parent there will be a compile time error
    this.j=j;
  }
}

EDIT:

, , arrLength arr[] check[], arrLength 0.

int arr[];
boolean check[];

, arrLength, .

arr = new int[arrLength];
check = new boolean [arrLength];
+6

, PalindromeArray. . ( PalindromeArrayUser) int.

, . (super(params))

+3

, PalindromeArray, . .

B , . , . - , .

:

+2
source

All Articles