1

I am a beginner in Java programming and I have encountered an problem. If you look at my code: `package whileloops;

import java.util.Scanner;

public class Empty_The_Piles {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int pileA, pileB, pileC;
        pileA = 3;
        pileB = 3;
        pileC = 3;
        int piletotal = pileA + pileB + pileC;


        while(piletotal >= 0){

            String pile = "";
            System.out.println("A: " + pileA + "\tB: " + pileB + "\tC: " + pileC);
            System.out.println("\nChoose a pile: "); 
            pile = keyboard.nextLine(); //After 1 loop it jumps over this line or something..

            System.out.println("How many fishes do you want to remove from pile " + pile + "?");
            int amount = keyboard.nextInt();

            if(pile.equals("A"))
                pileA = pileA - amount;
            else if(pile.equals("B"))
                pileB = pileB - amount;
            else if(pile.equals("C"))
                pileC = pileC - amount;
            else
                System.out.println("ERROR. NO SUCH PILE!");
        }
        System.out.println("All piles are empty! Nice work!");
    }

}

EDIT: When the code is on it's second loop it suddenly jumps over it. When I run the code the first time it pick's it up fine, but when it loops back to the start it jumps over pile = keyboard.nextLine();

It jumps directly to the next line... It doesn't wait for an input

If I try what the other thread you said was the answer, I need to type it in two times.

Guess this is easy for most of you :P

  • Thanks for the help :)
Joakimeg
  • 11
  • 2
  • This isn't a syntax error – byxor Jan 07 '17 at 14:59
  • I edited it. I do not see this as an duplicate... Tried what they said on the other one, but that makes the code more weird.... – Joakimeg Jan 07 '17 at 15:58
  • This is definitely a duplicate of the linked question. The issue is that `int amount = keyboard.nextInt();` doesn't consume the ending line break, so the subsequent `nextLine()` does it and returns immediatly. – Tunaki Jan 07 '17 at 17:38
  • When I added nextLine(); on the line under I need to type it two times... Is there something I am missing? – Joakimeg Jan 08 '17 at 18:56

0 Answers0