One Glove
One Glove

Reputation: 79

Returning values from a linked list without moving the iterator

I have 2 classes: Student and Registry.Registry is designed to hold a list of students. When I try to return valuse of the students from the registry class, the iterator moves to the next student to get the next value hence when only 2 students are present I get a NoSuchElemennt exception.

public String format() 
{
    ListIterator<Student> iterator 
            = studentList.listIterator();

    for (int i = 0; i < studentList.size(); i++)
    {
        System.out.println("if Statement");
        if (iterator.hasNext())
        {
            System.out.println("hasNext");
            return String.format("%0s%0s%0s", 
            iterator.next().getSurName(), iterator.next().getForeName(),
            iterator.next().getStudentId());
        }

    }        

    return null;
}

I'd like it to return all 3 values from stud1, then move on to stud2 and so on.

Here is a link to the Student and Registry source files and a tester for both; https://www.dropbox.com/sh/ol2jyfbuyyjnrcg/1J4LlEaqc6

I will post more code if it's requested.

Since the answer is closed, no more code and the files at the link have been removed.

Upvotes: 1

Views: 1068

Answers (3)

IndoKnight
IndoKnight

Reputation: 1864

To iterate around a list without using an Iterator, you should use Enhanced for loop. Then your code becomes -

public String format() 
{
ListIterator<Student> iterator 
        = studentList.listIterator();

for (Student student: studentList)
{
    System.out.println("For Loop");       

        return String.format("%s%s%s", 
        student.getSurName(), student.getForeName(),
        student.getStudentId());     

}   
 return null;   
}

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

You should call the next method only once, because every call forwards the cursor.

if (iterator.hasNext()) {
    Student currStudent = iterator.next();
    //use currStudent instead of calling next

I'd like it to return all 3 values from stud1, then move on to stud2 and so on.

Since your iterator is local to the method, it will be gone once the method returns. I think it will better to make your format method a static (utility) method and change it's signature to take an Student as a paramter. public static String format(Student student). That way you can format all the students while iterating through the list.

Update


You can write a utility method as follows:

public static String format(Student student) {
    return String.format("%0s%0s%0s", student.getSurName(), student.getForeName(), student.getStudentId());
}

And for a method that can format all the students and return the formats -

public List<String> formatAll() {
    ListIterator<Student> iterator 
            = studentList.listIterator();
    List<String> formats = new ArrayList<String>();//to keep formats of all students
    //for (int i = 0; i < studentList.size(); i++)
    //{
        //System.out.println("if Statement");
        while (iterator.hasNext()) //while loop is better for iterators
        {
            Student currStudent = iterator.next();
            formats.add(format(student)); //calling utility method here
        }

    //}        

    return formats; // finally return the list of formats
}

Upvotes: 1

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46778

You are advancing the iterator every time you call .next(). Call it just once for the current iteration.

Like so:

Student res = iterator.next();
return String.format("%0s%0s%0s", res.getSurName(),res.getForeName(),res.getStudentId());

Upvotes: 2

Related Questions