Hello Together i have the following problem. I have a main class (Wohnungsverteiler) where two methodes from the class Teilnehmer should be executed. The methodes have to be exceuted together because the methode anzahlTeilnehmer set the maximum of members for the while loop. I alawas get a noSuchElement Exception and i dont konw why?
Class Wohnungsverteiler:
public class Wohnungsverteiler {
private static Teilnehmer teilnehmer;
private static int anzahlTeilnehmer;
public static void main(String[] args) {
teilnehmer = new Teilnehmer();
teilnehmer.addTeilnehmer(teilnehmer.anzahlTeilnehmer(anzahlTeilnehmer));
}
}
Class Teilnhemer:
import java.util.ArrayList;
import java.util.Scanner;
public class Teilnehmer {
private ArrayList<String> teilnehmerListe;
public Teilnehmer() {
}
public int anzahlTeilnehmer(int anzahlTeilnehmer) {
Scanner sc2 = new Scanner (System.in);
System.out.println("Anzahl Teilnehmer:");
String TeilnehmerString = sc2.next();
anzahlTeilnehmer = Integer.parseInt(TeilnehmerString);
sc2.close();
return anzahlTeilnehmer;
}
public void addTeilnehmer(int anzahlTeilnehmer) {
teilnehmerListe = new ArrayList<String>();
Scanner sc1 = new Scanner(System.in);
int index = 0;
while (index < anzahlTeilnehmer) {
System.out.println("Teilnehmer:");
teilnehmerListe.add(sc1.next());
index++;
}
sc1.close();
}
public ArrayList<String> getTeilnehmerListe() {
return teilnehmerListe;
}
}
Error when run the program:
Anzahl Teilnehmer:
3
Teilnehmer:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Teilnehmer.addTeilnehmer(Teilnehmer.java:30)
at Wohnungsverteiler.main(Wohnungsverteiler.java:9)
Why do i get a NoSuchElementError??