1

I'm using Scanner object to read values from console. If i'm reading 2 strings in a row, everything works fine:

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the first string");
    String first = sc.nextLine();

    System.out.println("Enter the second string");
    String second = sc.next();

But if i'm trying to read Integer first and then these two Strings:

 Scanner sc = new Scanner(System.in);
    System.out.println("Enter num");
    int a = sc.nextInt();
    System.out.println("Enter the first string");
    String first = sc.nextLine();

    System.out.println("Enter the second string");
    String second = sc.nextLine();

    System.out.println("First '" + first + "'; second '" + second + "'");

Then line String first = sc.nextLine(); is not written in variable, skipped somehow =/

Check the console output:

Enter num
123                   //(it is input)
Enter the first string
Enter the second string //(This and previous line appear together)
Str                   //(Only one line appears for input)

First ''; second 'Str'

In debug i'm offered to enter first string, i put it, and on the next step debugger shows this variable as empty string.

Should it work like this??

0 Answers0