0

I was learning about input in Java. I used IntelliJ to write and run my code. But it keeps skipping a line. I run that line separately, and it worked fine.

The skipped line is:

String fullName = scanner.nextLine().trim();

Full code:

class main {
    public static void main(String args[]) {
        // 16- Reading Input
        System.out.println("\n16- Reading Input");

        Scanner scanner = new Scanner(System.in);
        System.out.print("Input your age: ");
        byte age = scanner.nextByte();
        System.out.println("You are " + age + " years old.");

        System.out.print("\nInput your name: ");
        String name = scanner.next();
        System.out.println("your name is: " + name);

        System.out.print("\nFull Name: ");
        String fullName = scanner.nextLine().trim();
        System.out.println("You are " + fullName);
    }
}

The result is like below:

16- Reading Input
Input your age: 35
You are 35 years old.

Input your name: kevin
your name is: kevin

Full Name: You are
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
K E
  • 3
  • 2
  • 1
    Note: this has nothing to do with IntelliJ. You will see the same behavior when running this code on the command line or in any other editor. – Code-Apprentice Apr 28 '22 at 22:33

0 Answers0