Skip to content
Advertisement

How to take input in java where array length is not defined?

My input is in this format:

JavaScript

The array length is not known. I coded it this way:

JavaScript

But I am getting this error.

JavaScript

Thanks in advance.

Advertisement

Answer

You should use hasNextInt to check for integer input. Once no more integers, then just use next() to read the player.

JavaScript

Example input’s supported

JavaScript

output

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement