2

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. Hope anybody could help. Maybe I should realize this completely different ? There is also another method called, "programmabruch()" which results in an endless loop. This method is calling itself the same way with this 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(){...}

which includes the same Menue like in this method with int steuerInt..

Pa.Don
  • 23
  • 5
  • Possible duplicate of [Scanner error with nextInt()](https://stackoverflow.com/questions/12832006/scanner-error-with-nextint) – Turing85 Apr 19 '18 at 15:55

1 Answers1

4

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();, will return the newline character as it is left in your scanner object after you do, this.max = myScanner.nextInt();, to avoid this issue I would rewrite those assignments using the Integer wrapper class.

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.

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
treebeard
  • 56
  • 4