BAEL-6460: Why is Scanner skipping nextLine() after using other next functions? (#14079)

This commit is contained in:
Azhwani 2023-05-22 09:04:42 +02:00 committed by GitHub
parent 2a2ac8d27f
commit a0931c50ce
1 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package com.baeldung.scanner;
import java.util.Scanner;
public class NextLineAfterNextMethods {
private static void produceSkippingNextLineMethod() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine(); // Skipped because it reads the remaining newline character
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV1() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV2() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = Integer.parseInt(scanner.nextLine());
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV3() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.skip("\r\n");
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
public static void main(String[] args) {
produceSkippingNextLineMethod();
fixSkippingNextLineMethodV1();
fixSkippingNextLineMethodV2();
fixSkippingNextLineMethodV3();
}
}