简体   繁体   中英

error: cannot find symbol and how to create and add objects to an arraylist

I'm attempting to create objects of my Profile class and store them in an arrayList created in a ProfileCollector class I created.

import java.util.ArrayList;

public class ProfileCollector
{
  private ArrayList<Profile> profileList;

  public ProfileCollector()
  {
    profileList = new ArrayList<Profile>();
    //peopleList = new ArrayList<String>();
  }
  public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
    profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));

  }
}

Here is my profile class:

import java.util.ArrayList;

public class Profile
{
  private ArrayList<DailyIntake> nutritionalStats;
  private String name;
  private int kcalTotal;
  private int proteinTotal;
  private int fatTotal;
  //These values represent nutritional requirements


  public Profile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal)
  {
    name = initName;
    kcalTotal = initKcalTotal;
    proteinTotal = initProteinTotal;
    fatTotal = initFatTotal;
  }

  public String getName(){
    return name;
  }
  public int getKcalTotal(){
    return kcalTotal;
  }
  public int getProteinTotal(){
    return proteinTotal;
  }
  public int getFatTotal(){
    return fatTotal;
  }

}

Here are the parts of my main.java that are important

public static void main(String[] args) {
    ProfileCollector profiles = new ProfileCollector();
    //theres also a line that calls to a method which calls to another method with this line: profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats));
}

The error message is that the variable profiles is cannot be found. My question is also if I am correctly creating objects and adding them to an ArrayList. I didn't know if creating a class was the best way to go about this, but it was the way I've partial seen before. Anything helps.

Here is the error message:

error: cannot find symbol profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats)); ^ symbol: variable profiles location: class Main

You can add a method in the ProfileCollector class, that accepts an Profile instance instead of 4 parameters. With this class you should be able to be able to add a new profile to your list and you should also be able to add a profile by typing in the 4 parameters, that are required to create a new Profile instance.

import java.util.ArrayList;

public class ProfileCollector
{
    private ArrayList<Profile> profileList;

    public ProfileCollector()
    {
        profileList = new ArrayList<Profile>();
        //peopleList = new ArrayList<String>();
    }
    public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
        profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));
    }

    public void addProfile(Profile profile) {
        profileList.add(profile);
    }
}

You need make sure to send by parameters the profiles variable to that function and then send it to the other function that you said it calls

profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats));

Also you are giving a variable type Profile to the addProfile,maybe you need to send only the profile atributes

profiles.addProfile(name, optimumCalories, optimumProteins, optimumFats);

Or change the addProfile to have a Profile by parameters that is the best practice

public void addProfile(Profile profile) {
        profileList.add(profile);
    }

If this is not the answer could you add some code from that functions, if you don't want to share the code put ...

Both Answeres above are good.

It is a compilation error so you need to learn how to interpret IDE error messages from the compiler. The problem is you are calling a method signature that does not exist.

Eather define the method

public void addProfile(Profile profile)

or call the existiong method:

profiles.addProfile("Profile1", 1650, 450, 350);

use "this" to refer to own properties in an Object.

 public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
        this.profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));
    }

Keep it up!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM