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?
There are two things you need to know:-
Firstly, you're using a
do-while
loop, which is an exit check loop, hence it will execute once atleast, before terminating. If you want to avoid it, try using awhile
loop.Next, to retrieve all elements from the list, you can either use the
list.get(index)
or thefor
loop traversal as below:-Update:-
The
for
you've seen above, is actually Foreach loop. This is what it meansHence, the loop means,
Now coming back to looping part. Forget everything, and just use a
break;
.After this piece of code
studentNumber=keybrd.next();
, do a check and break the loop.Simple solution.
Use
get(int)
method to retrieve an element.Use
add(int, E)
method to insert an element at an index.Why not use a hashmap with key as Student number and Object to that key as Student Object. In this way you could avoid duplication as well.
For the sentinel
The ArrayList is not an array... It's a List which uses arrays as its backing store.
The retrieval method from list is
list.get(index)
Alternately, you can use some syntactic sugar to just loop over them
You can add a
toString()
method to your Students class :And then you can say :
As far as this is concerned :
This behaviour is because you are using
do..while()
! Try usingwhile
instead."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:
or
to solve other issue, you can try: