-3

Has anyone idea about this code, where is the problem

import java.util.Iterator;
import java.util.NoSuchElementException;

public class AlbumIterator implements Iterator<Fotoablum>{
    Fotoalbum album;
    Foto aktuell;
    
    public AlbumIterator(Fotoalbum album){
        this.album=album;
        this.aktuell=aktuell;
    }
    public boolean hasNext(){
        if(this.aktuell == null){
            return true;
        }else{
            return this.aktuell.getNächstes() != null;
        }
    }
    public Foto next(){
        if(this.aktuell == null){
            this.aktuell = this.ablum.erstesFoto;
            return this.aktuell;
        }
        if(this.aktuell.getNächstes() == null){
            throw new NoSuchElementException("Keine weiteren Elemente vorhanden");
        }else{
            this.aktuell = this.aktuell.getNächstes();
            return this.aktuell;
        }
    }
    public void remove() {
        throw new UnsupportedOperationException("Diese Aktion wird nicht unterstützt.");
    }
    public static void main(String[] args){
        return;
    }
}

Error message

AlbumIterator.java:5: error: cannot find symbol
public class AlbumIterator implements Iterator<Fotoablum>{
                                               ^
  symbol: class Fotoablum
AlbumIterator.java:22: error: cannot find symbol
            this.aktuell = this.ablum.erstesFoto;
                               ^
  symbol: variable ablum

this is a homework about java iterator. I tried so many times, but it is still wrong, how can I make it works, do I need a main function?

Esoteriker
  • 41
  • 7
  • The error messages you are receiving can be auto-fixed by most modern Java IDEs. – Janez Kuhar May 29 '21 at 08:35
  • There seems to be a typo. `Fotoablum` should be `Fotoalbum`. – Janez Kuhar May 29 '21 at 08:37
  • And `this.ablum` should be `this.album`. Make sure you actually read the error message and research it, it's a lot quicker than asking here. – Henry Twist May 29 '21 at 08:48
  • No @LukasEder, but the first [result](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) on the error mentions checking for a typo as the first step, so reading and researching would have probably lead to a solution. – Henry Twist May 29 '21 at 08:59
  • watch the third and forth lines , The words are replaced incorrectly more over you can avoid this type of mistakes by using IDE like IntelliJ IDEA . – Lunatic May 29 '21 at 10:59

1 Answers1

0

A much easier solution using streams

You could save yourself the trouble and create one from a Stream:

Iterator<Foto> it =
Stream.iterate(
         album.erstesFoto, 
         f -> f.getNächstes() != null, 
         f -> f.getNächstes())
      .iterator();

Your specific compilation problems

AlbumIterator.java:5: error: cannot find symbol
public class AlbumIterator implements Iterator<Fotoablum>{
                                               ^
  symbol: class Fotoablum

My german-fu says you have a typo in your class name, though, you mean to iterate over Foto anyway, not Fotoalbum

AlbumIterator.java:22: error: cannot find symbol
            this.aktuell = this.ablum.erstesFoto;
                               ^
  symbol: variable ablum

Same typo, this time with the local variable.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • I actually downvoted when your answer only consisted of the streams suggestion. I presume you've then edited it during the grace period, so unfortunately I can't retract my vote as the answer hasn't technically been edited. I would be happy to retract if I could. – Henry Twist May 29 '21 at 09:07
  • we are not allowed to use Stream to solve this question, but thanks – Esoteriker May 29 '21 at 18:12