//main method
int number  = 1;
int num = 3;
details[] Darray = new details[number][num];

String name = JOptionPane.showInputDialog(null,
                "Enter the name",
                "Enter details",
                 JOptionPane.QUESTION_MESSAGE);
String DOB = JOptionPane.showInputDialog(null,
                "Enter the DOB",
                "Enter details",
                 JOptionPane.QUESTION_MESSAGE);

class details
// i have default construtors aswell
String getName(){
    return name;
                }

heres the problem - i was told that you are only ment to input data in the main method. I need a 2d array to store many people info.

I tried Darray[0] = name; but this came up

C:\Documents and Settings\java:34: incompatible types
found   : java.lang.String
required: details

since classes are ment to store information (possibley privately) I was wondering the easiest way to store it in a array and pass it. I also want to use the array in the main method after the have finished storing it.
any ideas?

thanks for looking

Dani AI

Generated

— the compile error came from trying to put a String into an array slot that holds objects. and were on the right track about fixing the declaration and class naming. Here are a couple of practical patterns to store people cleanly and to pass that structure into other classes.

A simple immutable data holder that accepts the values at construction keeps the object simple and avoids later setter/initialization issues:

public final class Person {
    private final String name;
    private final java.time.LocalDate dob;

    public Person(String name, java.time.LocalDate dob) {
        this.name = name;
        this.dob = dob;
    }

    public String getName() { return name; }
    public java.time.LocalDate getDob() { return dob; }
}

Create and populate a 2‑D array (or pass it to another class) by constructing Person instances and assigning them into array slots:

int rows = 10, cols = 3;
Person[][] table = new Person[rows][cols];
table[0][0] = new Person("Alice", java.time.LocalDate.parse("1990-05-12"));

public class Registry {
    private final Person[][] people;
    public Registry(Person[][] people) { this.people = people; }
    // methods that operate on the array...
}

Alternatives and cautions: use a List<Person> or List<List<Person>> if the number of entries changes at runtime — collections are simpler than fixed 2‑D arrays. Parse DOBs into java.time.LocalDate instead of keeping raw strings (see java.time.LocalDate). Always check for null before dereferencing array elements or construct objects as you create the array to avoid NullPointerException.

Recommended Answers

All 3 Replies

Darray is a multidimential array of class details so you neede to use Darray[0][0] something like that in order to access details object in Darray.
Darray[0][0] = name won't work because java compiler can't convert string to details.
i am not sure but you might need to initilize every object in the array before you are going to use them. May be java compiler does this for you by calling default constructer for each element in array...

Java will initialise all elements of an array to null when you create the array itself.

Your Details (ALWAYS, ALWAYS, ALWAYS capitalise classnames) class should have a public setName(String name) method.
You should indeed also initialise the array elements yourself.

primitive type like int, char it is not assigned to null but zero or space, when creating arrays of these types. so,
int[] x = new int[5]; //all elements are equals to zero....

but like jwenting said when you make an array of objects they are all set to null, you need to construct each of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.