Jump to content

[Java] - Constructor cannot be applied to given types?

Elijah Kamski
Go to solution Solved by Avocado Diaboli,

Remove the void keyword from both constructors, that should do the trick. Constructors don't require a return type in Java.

Hi there,

 

I'm making a piece of code that simply stores objects of the class Users.

When running the code, I get this:

image.thumb.png.1f08dceda6d95c81620bbc3d63c2ccff.png


Code:

array.add(new Users(name, relationship, id, trust, happiness, sadness, anger, fear, suprise, disgust));

 

I have tried searching up the issue on how to solve it, but most of it just state that hey, you have to have the correct amount of arguments and all, so you cannot have just name and relationship when you're trying to pass on id, trust and so on.

I have tested this with the default constructor and it works perfectly.
 

array.add(new Users());

The default constructor is the following.
 

	public void Users()
	{
		// Users are unknown by default
		this.name = null;
		this.relationship = "Stranger";
		this.id = -1;
		
		/* Emotion stats
		
		0 means that the individual does not feel the emotion at all.
		
		10 means that the individual feels that emotion strongly.
		
		Scale: 0-10
		
		*/
		this.trust = 0;
		this.happiness = 5;
		this.sadness = 0;
		this.anger = 0;
		this.fear = 0;
		this.suprise = 0;
		this.disgust = 0;
	}

 

The following is the non-default constructor, and the one that I am trying to make an object with.

	public void Users(String name, String relationship, int id, int trust, int happiness, int sadness, int anger, int fear, int suprise, int disgust)
	{
		// Users loading in previous state
		this.name = name;
		this.relationship = relationship;
		this.id = id;
		this.trust = trust;
		this.happiness = happiness;
		this.sadness = sadness;
		this.anger = anger;
		this.fear = fear;
		this.suprise = suprise;
		this.disgust = disgust;
	}


The following code is from the FileHandler class and is the one that stores the objects into the array list.

		while (scan.hasNextLine() == true)
		{
			StringTokenizer line = new StringTokenizer(scan.nextLine(),",");
			String name = line.nextToken().trim();
			String relationship = line.nextToken().trim();
			int id = Integer.parseInt(line.nextToken().trim());
			int trust = Integer.parseInt(line.nextToken().trim());
			int happiness = Integer.parseInt(line.nextToken().trim());
			int sadness = Integer.parseInt(line.nextToken().trim());
			int anger = Integer.parseInt(line.nextToken().trim());
			int fear = Integer.parseInt(line.nextToken().trim());
			int suprise = Integer.parseInt(line.nextToken().trim());
			int disgust = Integer.parseInt(line.nextToken().trim());
			array.add(new Users(name, relationship, id, trust, happiness, sadness, anger, fear, suprise, disgust));
			//array.add(new Users());
			lineCount++;
		}


As you can see from the code above, the amount of arguments that I am pushing through to the Users class to create a Users class object is the same, and with that, the data type as well is the same.

I have no idea what is going on and do not know how to solve it, please, if there is any help, I am grateful.

Thanks,
Eli

Link to comment
Share on other sites

Link to post
Share on other sites

Remove the void keyword from both constructors, that should do the trick. Constructors don't require a return type in Java.

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Avocado Diaboli said:

Remove the void keyword from both constructors, that should do the trick. Constructors don't require a return type in Java.

Ty for the help! 😄

It works now, didn't know it was that simple, forgot all about that stuff XD

Ty again for the help! 😄

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×