Java Java Data Structures Efficiency! Add tags to a course

Kevin Lankford
Kevin Lankford
1,940 Points

Having trouble initializing the Set in the constructor

I seem to be getting this error when attempting to initialize the constructor:

JavaTester.java:67: error: constructor Course in class Course cannot be applied to given types; Course course = new Course("Java Data Structures"); ^ required: String,Set found: String reason: actual and formal argument lists differ in length Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

I looks like Set is not being seen as String is the only object being found, what am I doing wrong in the initialization?

com/example/model/Course.java
package com.example.model;

import java.util.List;
import java.util.Set;

public class Course {
  private String mTitle;
  private Set<String> mTags;

   public Course(String title, Set<String> tag) {
    mTitle = title;
    tag = mTags;
    // TODO: initialize the set mTags
  }

  public void addTag(String tag) {
    // TODO: add the tag
  }

  public void addTags(List<String> tags) {
    // TODO: add all the tags passed in
  }

  public boolean hasTag(String tag) {
    // TODO: Return whether or not the tag has been added
    return false;
  }

  public String getTitle() {
    return mTitle;
  }

}

3 Answers

You just need to change it to:

mTags = tag;
// tag = mTags is incorrect, becuase you wouldn't be initializing your mTags var.

Now it should compile fine. Let me know if you need any other help.

Kevin Lankford
Kevin Lankford
1,940 Points

I seem to still be getting that error. I'm not quite sure why it's reacting as if I have not added the Set object to the constructor (error mentions "actual and formal argument lists differ in length")

JavaTester.java:67: error: constructor Course in class Course cannot be applied to given types; Course course = new Course("Java Data Structures"); ^ required: String,Set found: String reason: actual and formal argument lists differ in length Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

If you post all of your source code, meaning the other java classes involved in the project I can help you out.

your link is broken