Results 1 to 3 of 3
  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default How can one create an array of objects of class type?

    I'm really new to object/class concepts and already having difficulties with applying them. Could please sombody explain/show only the method below how to create and return an array of Exam objects? I need to get a data from a textfile which is passed to the method. Thanks in advance! I'd really appreciate it!

    Java Code:
    public Exam(String firstName, String lastName, int ID, String examType, int score)
    {
    this.firstName = firstName;
    this.lastName = lastName;
    this.ID = ID;
    this.examType = examType;
    this.score = score;
    }
    
    public static Exam[] readAllExams(Scanner s)
    {
    Exam[] object = new Exam[5];
    
    for(int i = 0; i < object.length; i++)
    {
    while (s.hasNext())
    {
    object[i] = s.next();
    }
    }
    return object;
    
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: How can one create an array of objects of class type?

    how to create and return an array of Exam objects?
    The posted code creates an array of Exam objects
    and returns that array.

    What questions do you have about what it does?
    Do you know how to use the new statement to create an instance of a class?
    Do you know how to assign a reference to an instance of a class to an array slot?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: How can one create an array of objects of class type?

    Line #18 doesn't make sense: the s.next() method returns a String and you are trying to assign it to something that has type Exam ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 02-10-2015, 07:08 PM
  2. create a static array of objects in java?
    By aidanmack in forum New To Java
    Replies: 10
    Last Post: 01-02-2014, 05:28 PM
  3. Replies: 1
    Last Post: 10-16-2011, 10:58 AM
  4. Replies: 6
    Last Post: 02-12-2011, 07:56 AM
  5. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 03:14 AM

Tags for this Thread

Posting Permissions

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