How can I set variables from a class of ArrayLists to the Main class?

Michael Pnrs :

I have already created an object and tried to set some variables but I cannot find the type of the variables that should I create in order to do not have any problem.

The part of the main class:

                Menu menu = new Menu();
                scanner.nextLine();
                System.out.println("Give a code.");
                String code =  scanner.nextLine();
                menu.setCode(code);
                System.out.println("Give a main dish");
                String MainDish = scanner.nextLine();
                menu.setMainDishes(MainDish);
                System.out.println("Give a drink.");
                String Drink = scanner.nextLine();
                menu.setDrinks(Drink);
                System.out.println("Give a sweet.");
                String Sweet = scanner.nextLine();
                menu.setSweets(Sweet);  

And the Menu class:

  public class Menu {
  ArrayList<String> MainDishes = new ArrayList<>();
  ArrayList<String> Drinks = new ArrayList<>();
  ArrayList<String> Sweets = new ArrayList<>();
  private String code;

  public ArrayList<String> getMainDishes() {
      return MainDishes;
  }

  public void setMainDishes(ArrayList<String> mainDishes) {
      MainDishes = mainDishes;
  }

  public ArrayList<String> getDrinks() {
    return Drinks;
  }

  public void setDrinks(ArrayList<String> drinks) {
      Drinks = drinks;
  }

  public ArrayList<String> getSweets() {
      return Sweets;
  }

  public void setSweets(ArrayList<String> sweets) {
      Sweets = sweets;
  }

  public String getCode() {
      return code;
  }

  public void setCode(String code) {
      this.code = code;
  }

Compliler give an error because of the String variables that I created.

TheWhiteRabbit :

Your menu class contains ArrayList variables and you try to assign a String to them. Instead of:

menu.setMainDishes(MainDish);

Try:

menu.getMainDishes().add(MainDish);

Also, common convention in Java is to start variables with lowercase, eg mainDish.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149443&siteId=1