-1

Does Scanner take all the input from the console window at once or does it take it one by one?

For example:

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

int str = scan.nextLine();

float f = scan.nextFloat();

Does Scanner take all the input at once and then put the value one by one in the data member? Or does it take one input and put its value in the corresponding data member then take another?

NotZack
  • 514
  • 2
  • 9
  • 21

1 Answers1

2

Both, either, depends.

If you type 7 then press Enter, the code will assign n = 7 and str = ""1 and will then wait for more. If you then type 3.14 and press Enter, the code will assign f = 3.14.

System.in will receive user input when the user presses Enter, so although it is a stream of characters (bytes actually), they will arrive in blocks of 1 line at a time.

Scanner will therefore see that 1 line at a time too.

1) See Scanner is skipping nextLine() after using next() or nextFoo()?

Andreas
  • 138,167
  • 8
  • 112
  • 195