cancel
Showing results for 
Search instead for 
Did you mean: 

Iterate through ArrayList

Former Member
0 Kudos

Hi, just got a simple question concering the iteration of an arraylist:

(left out the not relevant code blocks)

<b>Contents of login.data</b>

-



Marry;Poppins;passwort;1. September 2006
Peter;Lustig;wohnwagen;1. September 2005

<b>Login Bean</b>

-



import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;

public class LoginBean extends ArrayList {

	/**
	 * 
	 * Variablendefinitionen
	 * 
	 */

	Login loginListe;

	public LoginBean() {
		checkLoginExistance;
	}
	
	public boolean checkLoginExistance() {
		
		ListIterator li = this.listIterator();
        while (li.hasNext()) {
        	
        	String vorname = this.loginListe.getVorname();
        	String nachname = this.loginListe.getNachname();
        	String passwort = this.loginListe.getPasswort();
        	String letzterLogin = this.loginListe.getLetzterLogin();
        	
            System.out.println("Vorname: "+vorname);
            System.out.println("Nachname: "+nachname);
            System.out.println("Passwort: "+passwort);
            System.out.println("LastLogin: "+letzterLogin);
            
            li.next();
        }
		
		boolean result = false;
		return result;
	}
}

The problem occurs during the hasNext-Iteration Block


while (li.hasNext()) {
..}

The two elements should be iterated and printed out on console, but unfortunately just the 2nd element is printed out twice:

<b>Console Output:</b>

-



Vorname: Peter
Nachname: Lustig
Passwort: wohnwagen
LastLogin: 1. September 2005
Vorname: Peter
Nachname: Lustig
Passwort: wohnwagen
LastLogin: 1. September 2005

Message was edited by:

Lars Paetzold

Accepted Solutions (1)

Accepted Solutions (1)

former_member182294
Active Contributor
0 Kudos

Something missing in your code.

Where your assinging the value to <b>this.loginListe</b> ?

And also hasNext() returns only the availability of the data in List, you need to change code to in the begging of while loop.

this.loginListe = (Login)li.next();

Regards

Abhilash

Former Member
0 Kudos

worked fine,

thx a lot Abhilash !

Answers (0)