24

I want to add an object to an ArrayList, but each time I add a new object to an ArrayList with 3 attributes: objt(name, address, contact), I get an error.

import java.util.ArrayList;
import java.util.Scanner;
public class mainClass {
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("Plz enter Name : ");
        String name = input.nextLine();
        System.out.println("Plz enter Address : ");
        String address = input.nextLine();
        System.out.println("Plz enter ContactNo : ");
        String contact = input.nextLine();


        ArrayList<Data> Contacts = new ArrayList<Data>();
        Data objt = new Data();
        Contacts.add(objt.Data(name, address, contact));
    }
}

Here, Data is the class of which I'm trying to create an object and pass it to an ArrayList.

public class Data {

        private String name = "";
        private String address = "";
        private String cell = "";


        public void Data(String n, String a, String c){

            name = n;
            address = a;
            cell = c;
        }
        public void printData(){

            System.out.println("Name\tAddress\tContactNo");
            System.out.println(name + "\t" + address + "\t" + cell);
        }
}
SpaceCore186
  • 586
  • 1
  • 8
  • 22
Johnfranklien
  • 525
  • 3
  • 5
  • 15

7 Answers7

47

You need to use the new operator when creating the object

Contacts.add(new Data(name, address, contact)); // Creating a new object and adding it to list - single step

or else

Data objt = new Data(name, address, contact); // Creating a new object
Contacts.add(objt); // Adding it to the list

and your constructor shouldn't contain void. Else it becomes a method in your class.

public Data(String n, String a, String c) { // Constructor has the same name as the class and no return type as such
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

Try this one:

Data objt = new Data(name, address, contact);
Contacts.add(objt);
Rahul
  • 44,383
  • 11
  • 84
  • 103
Umar Asghar
  • 3,808
  • 1
  • 36
  • 32
1
Contacts.add(objt.Data(name, address, contact));

This is not a perfect way to call a constructor. The constructor is called at the time of object creation automatically. If there is no constructor java class creates its own constructor.

The correct way is:

// object creation. 
Data object1 = new Data(name, address, contact);      

// adding Data object to ArrayList object Contacts.
Contacts.add(object1);                              
RubioRic
  • 2,442
  • 4
  • 28
  • 35
saibaba vali
  • 2,641
  • 1
  • 17
  • 17
0

You have to use new operator here to instantiate. For example:

Contacts.add(new Data(name, address, contact));
James Skemp
  • 8,018
  • 9
  • 64
  • 107
Prashant Bhagwani
  • 25
  • 1
  • 3
  • 10
0

The best way to pass data to class List,

private final List<electronic> Electronic = new ArrayList<electronic>();

public List<electronic> getElectronic() {
    return Electronic;
}

// creating list to store and display the Electronic items
public void StoreAndDisplayElectronic() {
    String[] EelctronicName = { "Laptop", "Television", "ElectricBike" };
    String[] ElectronicID = { "091", "202", "151" };
    Double[] EPrice = { 340.00, 280.00, 600.00 };
    int[] EAmt = { 5, 12, 4 };

    for (int i = 0; i < ElectronicID.length; i++) {
        this.Electronic.add(new electronic(EelctronicName[i], ElectronicID[i], EPrice[i], EAmt[i]));
    }
}
Rajeev Sreedharan
  • 1,753
  • 4
  • 20
  • 30
0

You don't return a Data Object when call 'Data()' method.

Change 'Data()' method to:

    public Data Data(String n, String a, String c){
        name = n;
        address = a;
        cell = c;
        return this;
    }
Joaquín
  • 1
  • 1
-1

change Date to Object which is between parenthesis

moha
  • 1