0

While reading from the buffer , eg.

`Scanner scan = new Scanner(System.in);
int variable = scan.nextInt();
double varDouble = scan.nextDouble();
String s = scan.nextLine();
// if this string has been entered just after an integer is entered, it 
//just shows empty output , might be a buffer clearing or something . 

` how the input is stored in the buffer ? how can the input buffer be cleared and what is the need?

  • _if this string has been entered just after an integer is entered_ Are you pressing the enter key twice ? – soufrk Jun 11 '18 at 15:09
  • The default delimiter is `\p{javaWhitespace}+` Try `Scanner scan = new Scanner(System.in).useDelimiter("\\n")` – Mark Jeronimus Jun 11 '18 at 15:15

1 Answers1

0

In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() doesn’t not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next().

Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine.

You can fire a blank Scanner#nextLine call after Scanner#nextInt to consume rest of that line including newline

achAmháin
  • 3,944
  • 3
  • 12
  • 39
amrender singh
  • 6,982
  • 3
  • 17
  • 25