Constructor in class cannot be applied to given types

user2901128

I ve got the following code using arrays to find some prim numbers. However, when trying to compile my user class PalindromeArrayUser it says - "Constructor in class cannot be applied to given types"

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

However, I have passed to the constructer an int value (the same way it was designed in my blueprint). I don't quite get where the problem comes from. Thanks.

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 where the problem comes from. 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();


}

 }
Thirumalai Parthasarathi

when you create a constructor for a class, there won't be any default constructor created for that class. so if you extend that class and if the subclass tries to call the no-arg constructor of its super class then there will be an compile-time error.

to demonstrate:

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:

to answer your question do this, don't assign the value arrLength to arr[] and check[] as arrLength would be 0 at that time.

so just declare them like this

int arr[];
boolean check[];

and in the constructor after you assign the input to arrLength put these statements.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Constructor cannot be applied to given types?

From Dev

Constructor in class cannot be applied to given types. Hope for assistance

From Dev

Error: Constructor Room in class Room cannot be applied to given types

From Dev

"Cannot be applied to given types" Error during PrintStream

From Dev

Java error: constructor in class cannot be applied to given types

From Dev

java constructor in class cannot be applied to given types

From Dev

constructor DepartmentChooser in class DepartmentChooser can not be applied to given types

From Dev

"constructor JSONParser in class JSONParser cannot be applied to given types" error occured

From Dev

Java Polymorphism "cannot be applied to given types"

From Dev

Java - error: constructor "constructor name" in class "class name" cannot be applied to given types;

From Dev

Issue with "Method in Class cannot be applied to given types"

From Dev

Android: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types

From Dev

constructor in class cannot be applied to given types android studio

From Dev

Construtor cannot be applied to given types

From Dev

Constructor in class cannot be applied to given types. Hope for assistance

From Dev

Constructor in class cannot be applied to given types

From Dev

Constructor cannot be applied to given types, and name should be declared

From Dev

Error Upon Compilation Java: "constructor LotteryTicket in class LotteryTicket cannot be applied to given types"

From Dev

java: constructor ranngeIpScanner in class cannot be applied to given types;

From Dev

error: method addOutcome in class OutcomesTable cannot be applied to given types

From Dev

Method edit in class data access cannot be applied to given types

From Dev

Issue with "Method in Class cannot be applied to given types"

From Dev

android: how to fix error constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types

From Dev

Java 'constructor in class cannot be applied to given types' 'required: no arguments found: String'

From Dev

method in class cannot be applied to given types; required: HashMap<String,Integer>

From Dev

Constructor cannot be applied to given types; required: Parcel; actual and formal argument lists differ in length

From Dev

new BufferedReader in class Files cannot be applied to given types

From Dev

Are Java constructors called upon creating a class? "constructor cannot be applied to given types" error when no object has been created

From Dev

Constructor in Class cannot be applied to gives types

Related Related

  1. 1

    Constructor cannot be applied to given types?

  2. 2

    Constructor in class cannot be applied to given types. Hope for assistance

  3. 3

    Error: Constructor Room in class Room cannot be applied to given types

  4. 4

    "Cannot be applied to given types" Error during PrintStream

  5. 5

    Java error: constructor in class cannot be applied to given types

  6. 6

    java constructor in class cannot be applied to given types

  7. 7

    constructor DepartmentChooser in class DepartmentChooser can not be applied to given types

  8. 8

    "constructor JSONParser in class JSONParser cannot be applied to given types" error occured

  9. 9

    Java Polymorphism "cannot be applied to given types"

  10. 10

    Java - error: constructor "constructor name" in class "class name" cannot be applied to given types;

  11. 11

    Issue with "Method in Class cannot be applied to given types"

  12. 12

    Android: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types

  13. 13

    constructor in class cannot be applied to given types android studio

  14. 14

    Construtor cannot be applied to given types

  15. 15

    Constructor in class cannot be applied to given types. Hope for assistance

  16. 16

    Constructor in class cannot be applied to given types

  17. 17

    Constructor cannot be applied to given types, and name should be declared

  18. 18

    Error Upon Compilation Java: "constructor LotteryTicket in class LotteryTicket cannot be applied to given types"

  19. 19

    java: constructor ranngeIpScanner in class cannot be applied to given types;

  20. 20

    error: method addOutcome in class OutcomesTable cannot be applied to given types

  21. 21

    Method edit in class data access cannot be applied to given types

  22. 22

    Issue with "Method in Class cannot be applied to given types"

  23. 23

    android: how to fix error constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types

  24. 24

    Java 'constructor in class cannot be applied to given types' 'required: no arguments found: String'

  25. 25

    method in class cannot be applied to given types; required: HashMap<String,Integer>

  26. 26

    Constructor cannot be applied to given types; required: Parcel; actual and formal argument lists differ in length

  27. 27

    new BufferedReader in class Files cannot be applied to given types

  28. 28

    Are Java constructors called upon creating a class? "constructor cannot be applied to given types" error when no object has been created

  29. 29

    Constructor in Class cannot be applied to gives types

HotTag

Archive