0

My while loop is supposed to run as long as the String Inputconfirmed is not set to "ja" at the bottom of the loop. Somehow however the loop keeps on running even when "ja" is entered. My code below, would appreciate input as to what I'm doing wrong here. Please note that I have already used .equals and .equalsIgnoreCase instead of .contains. Thank you in advance.

String Inputconfirmed = "nee";
   while(!Inputconfirmed.contains("ja")){
        System.out.println("\nHallo daar, welkom bij SPOOKTOCHT!\n"+
                "\nWat is je voornaam?"+
                "\n(Typ je naam en druk op 'ENTER')");
        String Firstname = scan.nextLine();
        System.out.println("Hoi "+Firstname+"!"+
                "\nWat is je achternaam?");
        String Lastname = scan.nextLine();
        System.out.println("Bedankt! En hoe oud is "+Firstname+" "+Lastname+"?");
        int Age = 0;
        while(Age < 1 || Age > 99){
            try{Age = scan.nextInt();
                 } catch (InputMismatchException e){
                 System.out.println("Je mag hier alleen een cijfer invoeren van 1 tot 99."+
                         "\nProbeer het nog eens.");
                 scan.nextLine();
             }
        }
        Character Player = new Character();
        Player.setFirstname(Firstname);
        Player.setLastname(Lastname);
        Player.setAge(Age);
        System.out.println("Dus jij bent: "+Player.getFirstname()+" "+Player.getLastname()+" en je bent "+Player.getAge()+" jaar oud."+
                "\nKlopt dit?");
        Inputconfirmed = scan.nextLine();   
        scan.nextLine();
        }
  • 1
    did you check the values of `Inputconfirmed` after you enter ja? my guess - this is the problem https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – Madhawa Priyashantha Oct 29 '17 at 11:15
  • thanks @FastSnail you were right. I just noticed your link also, will give it a try.. ? – Arjan Pelle Oct 29 '17 at 11:24
  • yes this happens when you use `nextLine()` followed by `nextInt()` – Madhawa Priyashantha Oct 29 '17 at 11:26
  • 1
    solved and indeed duplicate. have added scan.nextLine(); after the nextInt(); and removed scan.nextLine(); at the end as well, and all works as intended now. – Arjan Pelle Oct 29 '17 at 11:32

0 Answers0