Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default array of objects with user defined values

    i am trying to create an array of objects that accepts user defined values..
    each object in the array should be used to call the same function.. please help

    import java.io.*;
    import java.util.*;
    class array
    {
    public static void main(String args[]) throws IOException
    {
    int count,i;

    count=0;
    array1[] arr=new array1[10];
    //array1[0]=new array1(); what should be done here??
    try
    {
    Scanner s=new Scanner(System.in);
    for(i=0;i<10;i++)
    {
    System.out.println("enter the id,name,dept of employee");
    int empid=s.nextInt();
    String empname=s.next();
    String empdept=s.next();
    a[i].set(empid,empname,empdept);
    count++;
    }
    s.close();
    System.out.println("employee details");
    for(i=0;i<count;i++)
    {
    a[i].display();
    }
    }
    catch(Exception e)
    {
    System.out.println("error");
    }
    }
    }
    class array1
    {
    int empid;
    String empname,empdept;
    public void set(int id,String name,String dept)
    {
    empid=id;
    empname=name;
    empdept=dept;
    }
    public void display()
    {
    System.out.println("id="+empid);
    System.out.println("name="+empname);
    System.out.println("dept="+empdept);
    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    a[i].set(empid,empname,empdept);
    arr[i].display();

    What is a? Do you mean arr?
    And you have to create an object, otherwise you will get a NullpointerException!

    //in your loop!
    arr[i] = new array1();
    arr[i].set....

    Or what exactly was your question here? :D

  3. #3
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    tht was actually the object name.. i dont know how to use each object in an array to invoke the same function

  4. #4
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Lettings a user add new values to an array.
    By xquzive in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 11:53 AM
  2. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 03:35 PM
  3. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 01:02 AM
  4. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM
  5. RegardingSgould Enter in user defined inbox
    By Prashant.surwade in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-23-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •