Search code examples
javaclassobjectarraylistsentinel

How can I return a specific object (index) from an arraylist in Java?


I created a class called Student and assigned it 3 values (StudentNumber, Course, Mark); I then assigned a created student (s) to an arrayList called studentsCourses.

This way each student takes up an index in the array. But how do I get the students back OUT of the arrayList... if that makes sense? I have them stored, but I don't know how to recall the information so that I can see it.

public static void main (String[]args){
ArrayList<Student> studentsCourses= new ArrayList<Student>();
String studentNumber="~";
String course="~";
int mark=0;

Student pat;

Student s=null;

do{
  s=new Student();
  System.out.println("Please Enter Your Student ID (type quit to Quit):\n");
  studentNumber=keybrd.next();
  s.setStudentNumber(studentNumber);
  System.out.println("Please Enter Your Course ID:\n");
  course=keybrd.next();
  s.setCourse(course);
  System.out.println("Please Enter Your Mark:\n");
  mark=keybrd.nextInt();
  s.setMark(mark);
  s.printStates();
  studentsCourses.add(s);
}while(!studentNumber.equals("quit"));

System.out.println(studentsCourses.size());   
System.out.println(studentsCourses);

so if I wanted to assign the created student (pat) the state of index 1 how would I do that?

My one other issue (going along with this piece of code) is my sentinel. it does indeed quit when I type quit. but it finishes running through the "do" first. meaning I actually end up with a student who's number is "quit" course is whatever and mark is whatever.

How do I force it to break the do as soon as "quit" is typed?


Solution

  • "But how do I get the students back OUT of the arrayList... if that makes sense? I have them stored, but I don't know how to recall the information so that I can see it."

    To get the data from arraylist:

    for (Student student : studentsCourses) {
            System.out.println(student.getStudentNumber());
            System.out.println(student.getCourse());
            System.out.println(student.getMarkr());
        }
    

    or

    for (int i = 0; i < studentsCourses.size(); i++) {
            Student student = studentsCourses.get(i);
            System.out.println(student.getStudentNumber());
            System.out.println(student.getCourse());
            System.out.println(student.getMarkr());
        }
    

    to solve other issue, you can try:

          do{
              s=new Student();
              System.out.println("Please Enter Your Student ID (type quit to Quit):\n");
              studentNumber=keybrd.next();
              if(studentNumber.equals("quit"))
                  break;
              s.setStudentNumber(studentNumber);
              System.out.println("Please Enter Your Course ID:\n");
              course=keybrd.next();
              s.setCourse(course);
              System.out.println("Please Enter Your Mark:\n");
              mark=keybrd.nextInt();
              s.setMark(mark);
              s.printStates();
              studentsCourses.add(s);
            }while(true);