Home > Net >  How fix problem with Arraylist when data not accepted in Arraylist?
How fix problem with Arraylist when data not accepted in Arraylist?

Time:04-01

class Client {
    public static void AddList(User user) {
    }

    static class User {
        public String name;
        public String age;
        public String mail;


        public User(String name, String age, String mail) {
            this.name = name;
            this.age = age;
            this.mail = mail;
        }
    }


    static ArrayList<Object> list = new ArrayList<>();

    public void AddList(String name, String age, String mail) {
        list.add(new User(name, age, mail));
    }
}

public class Main {

    public static void main(String[] args) {
        System.out.println("Give info plz");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        String age = scanner.nextLine();
        String mail = scanner.nextLine();

        Client.AddList(new Client.User(name,age,mail));

        Client.list.add("dqdqd");
        Client.list.add(1);

        for(Object s : Client.list) {
            System.out.println(s);
        }


    }

}
 

This is a line when it doesn’t work for me, the program should take values and put them in an Arraylist, in general, the program should work like a database, take values and put them in an Arraylist, but this line does not work for me, please help.

public void AddList(String name, String age, String mail) {
        list.add(new User(name, age, mail));
    }
}

enter image description here as you can see in the screenshot, the data entered is simply lost

CodePudding user response:

This is because you haven't implemented the AddList(User user) method. You've only implemented the AddList(String name, String age, String mail)

public static void AddList(User user) {
}

Should be something like

public static void AddList(User user) {
   list.add(user);
}

To print the object you will need to create a toString() method for your object.

static class User {
    public String name;
    public String age;
    public String mail;

    public User(String name, String age, String mail) {
        this.name = name;
        this.age = age;
        this.mail = mail;
    }

    public String toString() {
        return name   ", "   age   ", "   mail;
    }
}

CodePudding user response:

You have two AddList(User) methods - one is a class method (i.e. static) and the other is an instance method. You are calling the class method which has no body. Since you never instantiate an instance of Client there is no purpose to the instance method. Move the body from the instance method to the class method and remove the instance method.

class Client {
  static ArrayList<Object> list = new ArrayList<>();

  public static void AddList(User user) {
    list.add(new User(name, age, mail));
  }

  static class User {
    public String name;
    public String age;
    public String mail;

    public User(String name, String age, String mail) {
      this.name = name;
      this.age = age;
      this.mail = mail;
    }
  }
}
  • Related