Back to Problem Solutions forum
Hi everyone, I'm trying to solve 14 task, but it has random inputs. So I don't know how to perform such input. I made this:
// ...
ch = in.nextLine().charAt(0);
int numb = 0;
numb = in.nextInt();
// ...
// I'm sorry, but I removed non-important parts to prevent spoiling solution - Admin :)
I have found this solution on line
while ( in.hasNextInt() ) {
in StackOverflow, but that doesn't work for me. I tried to test it without switches, but it just prints empty line. Or doesn't print anything, because it doesn't get anything. I don't know :)
So how can I input array if I don't know when it ends? I guess line
while ( in.hasNextInt() ) {
works, but lines
ch = in.nextLine().charAt(0);
numb = in.nextInt();
has issues.
Thank you :)
Well. I changed
ch = in.nextLine().charAt(0);
on
ch = in.next().charAt(0);
and it works. :)
Magic? :D
Ha-ha :)
I'm glad you figured this out yourself!
No magic - nextLine
just swallowed the integer you wanted to read with nextInt
few lines below. Usually you
should not mix nextLine
with other nextXXX
methods of the Scanner. Probably you may want to read
documentation on it to make these things more clear: