S
S
SandPie2021-08-05 18:00:24
Java
SandPie, 2021-08-05 18:00:24

Is it possible to type "nothing" into Scanner?

I am writing a program with an infinite loop for myself and I want that when I do not enter anything into scanner.nextInt() (that is, I just press enter without entering any number), the loop ends its work, is it possible to do this? If so, how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-08-05
@SandPie

We read the line through scanner.nextLine().
If it is empty, we exit.
If not, we try to get a number through Integer.parseInt().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question