1

I am new to Java and I am starting to work with ArrayLists. What I am trying to do is create an ArrayList for students. Each student has different attribute associated with them (name, id). I am trying to figure out how to add a new student object with this attributes. Here is what I have:

ArrayList < Student > studentArray;
public Student(String name, int id) {
  this.fname = name;
  this.stId = id;
}
public Stromg getName() {
  return fname;
}
public int getId() {
  return stId;
}
public boolean setName(String name) {
  this.fname = name;
  return true;
}
public boolean setIdNum(int id) {
  this.stId = id;
  return true;
}
3
  • So what is really your question? What is going wrong? Commented Mar 13, 2013 at 4:51
  • How do I create a new object (Student) with a name and id associated with it that the user inputs? Commented Mar 13, 2013 at 4:52
  • I presume Stromg means String, or this wouldn't compile (unless you actually had a Stromg class lurking around). Commented Mar 13, 2013 at 5:19

3 Answers 3

6

What you need is something like the following:

import java.util.*;

class TestStudent
{
    public static void main(String args[])
    {
        List<Student> StudentList= new ArrayList<Student>();
        Student tempStudent = new Student();
        tempStudent.setName("Rey");
        tempStudent.setIdNum(619);
        StudentList.add(tempStudent);
        System.out.println(StudentList.get(0).getName()+", "+StudentList.get(0).getId());
    }
}

class Student
{
    private String fname;
    private int stId;

    public String getName()
    {
        return this.fname;
    }

    public int getId()
    {
        return this.stId;
    }

    public boolean setName(String name)
    {
        this.fname = name;
        return true;
    }

    public boolean setIdNum(int id)
    {
        this.stId = id;
        return true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You instantiate a Student object by passing the appropriate values to the constructor.

Student s = new Student("Mr. Big", 31);

You place elements into an ArrayList (or List) by using the .add() operator.*

List<Student> studentList = new ArrayList<Student>();
studentList.add(s);

You retrieve user input via the use of a Scanner bound to System.in.

Scanner scan = new Scanner(System.in);
System.out.println("What is the student's name?");
String name = scan.nextLine();
System.out.println("What is their ID?");
int id = scan.nextInt();

You repeat this with a loop. That portion shall be left as an exercise to the reader.

*: There are other options, but add() simply adds it to the end, which is typically what you want.

4 Comments

Thank you for the response. I get exactly what you are saying but what purpose are my get/set methods serving in my project if I put the values in like you say?
Accessors and mutators. You do want to access the value of their name and their ID, but do you really need to change them? It may be worth removing the setters on those.
@bardockyo The values are not yet in Student object you need to put them using setters
@AbdullahShaikh: No; considering the way that the object is constructed, it would be impossible for a value to not be in Student (unless null and -1 were passed, and even then those are still values).
1
final List<Student> students = new ArrayList<Student>();
students.add(new Student("Somename", 1));

... and so on add more students

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.