0

I have this code:

public static void main(String[] args) {
    double Guillermo, Luis, Juan;
    Scanner in = new Scanner(System.in);
    String exit = "";

    do {
        System.out.print("Cuanto tiene Guillermo: ");
        Guillermo = in.nextDouble();

        Luis = Guillermo / 2;
        Juan = (Guillermo + Luis) / 2;

        System.out.println("Guillermo tiene: " + Guillermo + " dolares");
        System.out.println("Luis tiene: " + Luis + " dolares");
        System.out.println("Juan tiene: " + Juan + " dolares");

        System.out.println("Desea repetir: Y/N ");
        exit = in.nextLine();

    } while (exit.equals("y"));

}

So, when i run the program, works fine, but in the last line "Desea repetir: Y/N" Doesn't ask to me, just jump this part and the program ends.

paul
  • 11
  • 4
  • Try to add "in.nextLine();" before "exit = in.nextLine();". – yoav Mar 01 '19 at 19:27
  • Yes it worked, but why does that happen? – paul Mar 02 '19 at 19:27
  • You consume the \n character, more info: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo . https://stackoverflow.com/questions/12265216/why-cant-i-enter-a-string-in-scannersystem-in-when-calling-nextline-method – yoav Mar 03 '19 at 21:29

0 Answers0