java: 在方法 B 中调用方法 A,该方法最初在 A 中被调用,NoSuchElementException

java: Calling method A in method B that is initially callid in A, NoSuchElementException

我正在编写一个 java 应用程序,它可以写入一个文本文件。

主要是扫描仪要求用户提供一些选项。其中之一是在文件中添加某些内容。这是通过调用

static void OptionMenu() 

方法,它利用了 switch case 算法。

通过选择某个选项,下一个方法(例如写入文件),

static void write() 

被调用。 此方法使用扫描仪。

现在如果我添加代码

OptionMenu() 

(到 return 到选项菜单)在 write() 的末尾,代码运行到这一行,我得到以下错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)

但是,如果我删除最后的代码以再次调用方法 OptionMenu(),我不会收到任何错误,并且会写入所需的文件。

但是,我希望能够返回到选项菜单并为用户提供执行另一个选项的可能性,所以我不希望应用程序退出。

希望有人能提供解决方案。请参阅下面的相关代码端口。 提前致谢!

此致,

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

public class WoningApplicatie 
{ 
 private static Portefeuille portefeuille = new Portefeuille();

public static void main(String[] args)
{
    String options =
            "1) Voeg koopwoning toe" + "\n" +
            "2) Voeg huurwoning toe" + "\n" +
            "3) Laat huurwoningen zien" + "\n" +
            "4) Laat koopwoningen zien" + "\n" +
            "5) Sluit de applicatie";
    Println("Kies één van de volgende opties");
    Println(options);
    Println("");

    OptionMenu();

    //System.out.println(portefeuille.getWoningen().size());


}



/** Laat je uit één van de beginoptie's kiezen.
 * 
 */
static void OptionMenu()
{
    Scanner input = new Scanner(System.in);
    Print("Voer nu het optienummer in: ");
    int keuze = input.nextInt();

    switch (keuze)
    {

        case 1:
            //Voeg Koopwoning toe
            write();

            //try
            //{
                OptionMenu();
            //}catch(Exception e)
            //{ Println("kan toch niet?");
            //}

            break;
        case 2:
            Println(keuze);//Voeg huurwoning toe
            break;
        case 3:
            Println(keuze);//Laat Koopwoning zien
            break;
        case 4:
            Println(keuze);//Laat huurwoning zien
            break;
        case 5:
            Println(keuze);//Sluit de applicatie
            break;
        default:
            Println("Fout! Probeer het opnieuw. Kies één van de opties (1-5)");
            OptionMenu();
            break;
    }
    input.close();
}


static void write()
{
    Scanner woningToevoeg = new Scanner(System.in);

        try 
        {   
            Print("Straat: ");
            String straat = woningToevoeg.next();
            Print("Huisnummer: ");
            String huisnummer = woningToevoeg.next();
            Print("Postcode: ");
            String postcode = woningToevoeg.next();
            Print("Woonplaats: ");
            String woonplaats = woningToevoeg.next();
            //Adres 

            Print("aantal kamers: ");
            int kamers = woningToevoeg.nextInt();
            Print("koopprijs: ");
            int koopprijs = woningToevoeg.nextInt();
            //Woning

            Print("Energielabel (A-E): ");
            String energielabel = woningToevoeg.next();

            woningToevoeg.close();

            Adres adres = new Adres(straat, huisnummer, postcode, woonplaats);
            KoopWoning koopwoning = new KoopWoning(adres, kamers, koopprijs,"TE KOOP", energielabel);
            portefeuille.voegtoe(koopwoning);


            //This is the content to write into file
            String contentPre = portefeuille.getWoningen().size() + "\r\n";                 
            String content = koopwoning.getStatus() + "\r\n"
                    + adres.getStraat() + " " + adres.getHuisnummer() + "\r\n"
                    + adres.getPostcode() + " " + adres.getPlaats() + "\r\n"
                    + koopwoning.getKamers() + " kamers" + "\r\n"
                    + "vraagprijs " + koopwoning.getVraagPrijs() + "\r\n"
                    + "energiepeil " + koopwoning.getEnergiePeil() + "\r\n";

            File file = new File("portefeuille.txt");

            // if file doesnt exists, then create it
                if (!file.exists()) 
                {
                    file.createNewFile();
                }
            Scanner searchEmpty = new Scanner(file);

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(contentPre);
                if(searchEmpty.hasNextLine());
                {

                    String random = ""; 
                    while(!random.isEmpty())
                    {
                        random = searchEmpty.nextLine();
                    }
                    bw.write(content);
                }

            bw.close();
            searchEmpty.close();

            Println("U heeft een woning toegevoegd!");

        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }


    }
///... some other code: Println methods for example,
}

不关闭 'input' 扫描器和 'woningToevoeg' 扫描器阻止了错误的发生。