Task14 Problem

Back to Problem Solutions forum

transingularity     2015-06-29 08:41:50

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 :)

transingularity     2015-06-29 09:53:13

Well. I changed

ch = in.nextLine().charAt(0);

on

ch = in.next().charAt(0);

and it works. :)

Magic? :D

Rodion (admin)     2015-06-29 10:39:28
User avatar

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:

java.util.Scanner javadoc

Please login and solve 5 problems to be able to post at forum