简体   繁体   English

Scanner.nextInt()NoSuchElementException

[英]Scanner.nextInt() NoSuchElementException

I got this code (sorry for the german inside): 我得到了这段代码(对不起德国内部):

public void eingabewerte(){

    int steuerInt;
        steuerInt=-1;

    Scanner myScanner = new Scanner(System.in);
    System.out.println("Bitte geben Sie die maximal Augenzahl des Wuerfels an (Ganzzahl): ");

    try{
    this.max = myScanner.nextInt(); //liest den naechsten Integer Wert ein

    System.out.println("Bitte geben Sie an wie oft gewuerfelt werden soll (Ganzzahl): ");

    this.wurfzahl = myScanner.nextInt();

    System.out.println("Wollen Sie");
    System.out.println("----------");
    System.out.println("(1)die Eingabe neustarten                                               ");
    System.out.println("(2)einen Versuch mit den Werten: "+this.max+"|"+this.wurfzahl+" starten.");

   steuerInt = myScanner.nextInt();



    } catch (InputMismatchException e){ //wenn nicht Integer oder nicht der Radix entspricht oder outofRange
        e.printStackTrace();
        e.toString();
        System.out.println("Sie haben keinen Integer Wert oder einen zu großen/kleinen Integer Wert eingegeben!");
    } catch (IllegalStateException e) { //wenn der Scanner closed ist 
        e.toString();
        e.printStackTrace();
    }                                 //ende trycatch

   myScanner.close(); //Scanner schlieszen

    if (steuerInt == 1){
        System.out.println("----------------------------------------------------------------");
        System.out.println("Neue Eingabe");
        System.out.println("----------------------------------------------------------------");


        this.eingabewerte();        //eingabe neustarten

    } else if (steuerInt==2) {
        this.versuch(); 

    } else {

        System.out.println("Sie haben keine der erwarteten Steuerzeichen (Integer) 1 oder 2 eingegeben");
        this.programmabbruch();
    }

}

Now I get back if I'm typing to the console 现在我回到控制台

    Bitte geben Sie die maximal Augenzahl des Wuerfels an (Ganzzahl): 
1
Bitte geben Sie an wie oft gewuerfelt werden soll (Ganzzahl): 
2
Wollen Sie
----------
(1)die Eingabe neustarten                                               
(2)einen Versuch mit den Werten: 1|2 starten.
1
----------------------------------------------------------------
Neue Eingabe
----------------------------------------------------------------
Bitte geben Sie die maximal Augenzahl des Wuerfels an (Ganzzahl): 
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)
    at aufgabe1oopwuerfel.Wuerfeltest.eingabewerte(Wuerfeltest.java:37)
    at aufgabe1oopwuerfel.Wuerfeltest.eingabewerte(Wuerfeltest.java:69)
    at aufgabe1oopwuerfel.Aufgabe1OOPWuerfel.main(Aufgabe1OOPWuerfel.java:22)
/home/r/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1

I know the documentation of the Exception but I dont understand why it interrupts at this point. 我知道Exception的文档,但我不明白为什么它会在此时中断。 Hope anybody could help. 希望有人能帮忙。 Maybe I should realize this completely different ? 也许我应该意识到这完全不同? There is also another method called, "programmabruch()" which results in an endless loop. 还有另一种方法称为“ programmabruch()”,它会导致无限循环。 This method is calling itself the same way with this Integer Input Menue Style. 此方法使用此Integer Input Menue Style(整数输入菜单样式)以相同的方式调用自身。

EDIT: I tried now to prove that a Integer follows like 编辑:我现在试图证明一个整数遵循

if(myScanner.hasNextInt()){
 this.max = myScanner.nextInt();
}

And then i was trying 然后我在尝试

 if(myScanner.hasNextInt()){     
     this.max = Integer.parseInt(myScanner.nextLine());
    }

It results in an endless loop of the secon method public void programmabruch(){...} 这导致secon方法的无限循环public void programmabruch(){...}

which includes the same Menue like in this method with int steuerInt.. 其中包括与int steuerInt。相同的Menue。

When you do this 当你这样做

this.max = myScanner.nextInt(); //liest den naechsten Integer Wert ein

System.out.println("Bitte geben Sie an wie oft gewuerfelt werden soll (Ganzzahl): ");

this.wurfzahl = myScanner.nextInt();

your second integer assignment, this.wurfzahl = myScanner.nextInt(); 您的第二个整数分配, this.wurfzahl = myScanner.nextInt(); , will return the newline character as it is left in your scanner object after you do, this.max = myScanner.nextInt(); 完成后,将返回换行符保留在扫描仪对象中的位置, this.max = myScanner.nextInt(); , to avoid this issue I would rewrite those assignments using the Integer wrapper class. ,为避免此问题,我将使用Integer包装器类重写这些分配。

this.max = Integer.parseInt(myScanner.nextLine()); 
this.wurfzahl = Integer.parseInt(myScanner.nextLine());

This will take in the whole next line as a string then parse it and give you a nice clean integer. 这将把整个下一行作为一个字符串,然后解析它,并为您提供一个不错的干净整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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