Java cannot be applied to given types string and integer
Your EventInformation class needs a constructor like
public EventInformation(String eventTime, int eventDatum) {
this.eventTime = eventTime;
this.eventDatum = eventDatum;
}
It looks like you are expecting Java constructors to behave like Typescript ones. You always need the explicit constructor when initializing fields.
Java error: Cannot be applied to given types
You must declare a constructor for your Object
class inside it as:
public class Object {
String a1;
String[] a2;
int a3;
double a4;
long a5;
public Object(String example_text, String[] strings, int i, double v) {
}
}
And another important thing is that Object
is a predefined class in Java, so you should use full package name of your own Object
class in main method:
public class Main
{
public static void main(String[] args)
{
Object obj1 = new path.to.Object("example text", new String[] {"some", "more", "examples", "here"}, 1, 1.0);
}
}
Constructor Pair cannot be applied to given types; Required: Integer, String; found: no argument
In Java if you declare a class with a construct with parameters, then the "empty constructor" is not avaiable by default and you have to declare it by yourself.
So for example:
public class A {
public static void main(String[] args) {
A first = new A();
}
}
If you compile it works, but with this class:
public class B {
public B(int i) {
}
public static void main(String[] args) {
B second = new B();
}
}
you get a compile error
How to fix method cannot be applied to given types
First of all, the binarySearch method is made by three arguments, and you are not providing them, secondly you should make your binarySearch method static, otherwise it cannot be called from the main method without creating an instance first.
It should be like this, i think
public class Main {
public static void main(String[] args) {
args = new String[3];
args[0] = "100";
int z = Integer.parseInt(args[0]);
double k = (int)(Math.random() * 1000001);
int n = 1000000;
int arr[] = new int[n];
int i = 0;
for(i = 0;i<n;i++){
arr[i] = i;
}
long startTime = System.currentTimeMillis();
for(int t= 0; t<z; t++) {
binarySearch(n, k, arr);
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("It took " + elapsedTime + " ms to repeat the algorithm.");
}
static int binarySearch(int n, double k, int arr[]) {
int li = 0;
int re = n+1;
int m;
while (li < re-1) {
m = (li + re) / 2;
if (k <=arr[m]){
re = m;
}
else{
li = m;
}
}
return re;
}
}
EDIT: Now it works, but please check if your application logic to be sure it's doing what you are expecting to do
method in class cannot be applied to given types; required: HashMap<String,Integer>
You should pass map as a parameter in getPostDataString method
protected String doInBackground(Object... params) {
URL url;
String response = "";
try {
url = new URL("http://app.iseemobile.com/imenu/getDistrictRestaurants.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
Map<String, Integer> inputMap = new HashMap<String, Integer>();
map.put("district", 1);
writer.write(getPostDataString(map);
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
Sports team model - constructor in class cannot be applied to given types
Explanation
Your error message is pretty straightforward, carefully read it:
Error on line 12: constructor Game in class Game cannot be applied to given types;
return r.toString() + "\n" + super.play(new Game()) + "\n";
^required:
java.lang.String
found: no argumentsreason: actual and formal argument lists differ in length
So you are calling new Game()
, without any arguments. But the constructor in that class requires you to call it with a String
, like new Game("foo")
.
If you lookup the code for the class Game
, you will see something like:
public class Game {
...
// Constructor that requires a String as argument
public Game(String foo) {
...
}
...
}
Check out the class to see what exactly the purpose of that String
is.
Example
To give you a better feeling for what you did wrong, let me show you another example. Suppose you have a class Person
and you want that each person has a String name
and an int age
. You can achieve this by letting the constructor require both. For example:
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public String getAge() { return age; }
}
Now you can create instance of this class by calling the constructor and providing both, a name and an age:
Person person = new Person("John", 20);
But what you are trying to do is just calling it like new Person()
, without supplying any name or age, despite the constructor requiring it.
Method Cannot be Applied to Given Types Java
You didn't put the strDay
value. According to the method signature public static int dayToNumber(String strDay)
you must put one.
How do I fix "error: constructor (class) in class (class) cannot be applied to given types;"
There is no constructor in Bandmember
class that accepts a student as an argument. The constructor currently looks like this: public BandMember(String name, String email, String major, double gpa, int grade)
. I understand that you might want to create a new Bandmember
with a student
. Then you can create another constructor inside the Bandmember
class. Here is a template that should work.
public BandMember(Student student){
this.name = student.returnName();
this.email = student.returnEmail();
this.major = student.getMajor();
this.gpa = student.getGPA();
this.grade = student.getGrade();
System.out.println("created new band member");
}
Related Topics
Uninitialized Variables and Members in Java
Unsupportedoperationexception When Trying to Remove from the List Returned by Array.Aslist
Java 8 Nested (Multi Level) Group By
Drawing a Rectangle That Won't Disappear in Next Paint
Setting the Maximum Size of a Jdialog
The Literal Xyz of Type Int Is Out of Range
Java Stack Overflow Error - How to Increase the Stack Size in Eclipse
How to Count the Number of Matches for a Regex
Run Piece of Code Contained in a String
Getoutputstream() Has Already Been Called for This Response
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
How Can One Detect Airplane Mode on Android
Android Reading from an Input Stream Efficiently
Transitive Dependencies Not Resolved for Aar Library Using Gradle
Jersey /* Servlet Mapping Causes 404 Error for Static Resources