1

This is my code, the while loop does not have an input and the rep variable does not accept an input:

import java.util.Scanner;

public class MixedData {
    public static void main(String[] args) {
        String rep = "";
        do {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter your full name");
            String name = keyboard.nextLine();

            System.out.print("Enter your GPA: ");
            double gpa = keyboard.nextDouble();

            System.out.println("Name: " + name + ", GPA: " + gpa);
            System.out.println("Do you want to enter the data for another student?(y/n)");
            rep = keyboard.nextLine();
        } // This does not accept input
        while (rep.equals("y"));
    }
}
  • 1
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Progman Jul 14 '20 at 19:24

4 Answers4

1

Either just add one more keyboard.nextLine() before rep = keyboard.nextLine(); (in order to clear the newline character), or read your double gpa value with:

double gpa = Double.parseDouble(keyboard.nextLine());

Important point to understand here (especially if you're a novice Java developer), about why your code doesn't work, is, that you invoke nextDouble() as a last method on your Scanner instance, and it doesn't move the cursor to the next line.

A bit more details:

All the methods patterned nextX() (like nextDouble(), nextInt(), etc.), except nextLine(), read next token you enter, but if the token isn't a new line character, then the cursor isn't moved to the next line. When you enter double value and hit Enter, you actually give to the input stream two tokens: a double value, and a new line character, the double value is initialized into the variable, and the new line character stays into input stream. The next time you invoke nextLine(), that very new line character is read, and that's what gives you an empty string.

Giorgi Tsiklauri
  • 6,699
  • 7
  • 29
  • 54
0

You need to skip blank lines.

public static void main(String[] args) {
    String rep;
    Scanner keyboard = new Scanner(System.in);
    do {
        System.out.print("Enter your full name");
        String name = keyboard.nextLine();

        System.out.print("Enter your GPA: ");
        double gpa = keyboard.nextDouble();

        System.out.println("Name: " + name + ", GPA: " + gpa);
        System.out.println("Do you want to enter the data for another student?(y/n)");
        rep = keyboard.next();
        keyboard.skip("\r\n"); // to skip blank lines
    }
    while (rep.equalsIgnoreCase("y"));
    keyboard.close();
}
Ridwan
  • 151
  • 1
  • 12
0

Here's the same code using a while loop instead of do-while. It works the way you want it to.

import java.util.Scanner;

public class MixedData {
    public static void main(String[] args) {
        String rep = "y";

        while (!rep.equals("n")) {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter your full name: ");
            String name = keyboard.nextLine();

            System.out.print("Enter your GPA: ");
            double gpa = keyboard.nextDouble();

            System.out.println("Name: " + name + ",GPA: " + gpa);
            System.out.println("Do you want to enter the data for another student?(y/n)");
            rep = keyboard.next();
        }
    }
}
0

Use nextLine instead of nextDouble:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    String rep = "";
    do {
        System.out.println("Enter your full name:");
        String name = keyboard.nextLine();

        System.out.println("Enter your GPA:");
//        double gpa = keyboard.nextDouble();
        double gpa = Double.parseDouble(keyboard.nextLine());

        System.out.println("Name: " + name + ", GPA: " + gpa);

        System.out.println("Do you want to enter the data for another student?(y/n)");
        rep = keyboard.nextLine();
    } while (rep.equals("y"));
    keyboard.close();
}