简体   繁体   中英

Java Scanner reading same file over and over again

I know the problem already exists already on here but the solutions didn't really help me so far.

My problem is, that I have a file with 5 lines and want to ready those into a collection. However when I'm reading the Scanner reaches the end of the file its starts again right at the beginning and does that until the complies throws an error for too many open files.

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;

public class TerminManager {
    String name;
    private Collection<Termin> termine;
    public TerminManager() {
        this.setName("aName");
        this.termine = new ArrayList<Termin>();
    }

    private void setName(String name) {
        this.name = name;
    }
    private String getName(){
        return this.name;
    }

    public void loadTermine(String fileName)
    {

        FileReader reader = null;
        Scanner sc = null;

        try
        {
            reader = new FileReader(fileName);
            sc = new Scanner(reader);
        } catch (FileNotFoundException e)
        {
            System.err.println("Datei " + fileName + " nicht gefunden!");
            e.printStackTrace();
        }

        while(sc.hasNext() == true)
        {
            String zeile = sc.next();
            String delims = "[;]";
            String[] zeile_feld = zeile.split(delims);
            Termin aTermin = new Termin(
                    zeile_feld[0],
                    zeile_feld[1],
                    zeile_feld[2],
                    zeile_feld[3]);
            this.termine.add(aTermin);

        }
        sc.close();

    }

    public void printTermine() {
    }
}

This is the menu class where loadTermine is called

import java.util.Scanner;

public class Menu
{
    
    private static TerminManager tManager = new TerminManager();
    private static Scanner sc = new Scanner("System.in");
    
    public Menu()
    {
        
    }
    
    public static void printMenu()
    {
        System.out.print("A)\t Laden der Verfügbaren Termine aus einer Text-Datei\n");
        System.out.print("B)\t Anzeigen der Verfügbaren Termine\n");
        System.out.print("C)\t Anlegen eines neuen Patienten\n");
        System.out.print("D)\t Erfassen einer Impfstoffanlieferung\n");
        System.out.print("E)\t Anzeige der aktuell verfügbaren Impfdosen\n");
        System.out.print("F)\t erfassen einer Impfterminvereinbarung mit einem Patienten\n");
        System.out.print("G)\t Erfassen einer durchgeführten SChutzimpfung auf Basis eines zuvor erstellen Impftermins\n");
        System.out.print("H)\t Beenden des Programms\n");
        
        System.out.println("Bitte geben Sie den buchstaben des Menue-Punktes ein: ");
    }
    
    public static void auswahlMenu(String auswahl)
    {
        boolean stop = false;
        while( stop == false)
        { 
            switch(auswahl.toLowerCase())
            {
                case "a": tManager.loadTermine("Impftermine.txt");break;
                case "b":tManager.printTermine();break;
                case "c": createPatient();break;
                default: System.out.print("Bitte einen Buchstaben zwischen \"A\" und \"H\" eingeben!"); stop = false;break;
            }
        }
    }

    private static void createPatient() {

    }
}

this is the tester class, where the main function is

import java.util.Scanner;

public class Test {

    
    private static Menu menu = new Menu();
    private static Scanner eingabe = new Scanner(System.in);
    private static String weiter = "ja";
    private static String auswahl ="";
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
    while(weiter.equals("ja") || weiter.equals("j"))
    {
        menu.printMenu();
        auswahl = eingabe.next();
        menu.auswahlMenu(auswahl);
        auswahl.toLowerCase();
        if(!auswahl.equals("h"))
        {
            System.out.println("Moechten Sie weiter machen? (Ja/Nein)");
            weiter = eingabe.next();
            weiter.toLowerCase();
        } else 
        {
            weiter = "nein";
        }
        
        
    }
        
        
        
    }

}

This is the code I have to read the file. I read in other posts that In need to use the.next() function in order for it to work but It doesn't help in my case. Maybe Im just stupid and did it wrong.

Thanks for the help in advance!

Found the problem thanks to @RogerLindsjö!

The problem was that the auswahlMenu() function never ended and therefore was always starting to read the file. With changing the condition so that the while loop ends it works as intended now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM