Unit 2 Input - nextInt method

It seems that nextLine is not working properly when using a Scanner object especially when there are multiple inputs of different types. Anyone else had this issue and is there a workaround?

Hi @mjdeister, thank you so much for you question as I’m sure other teachers in the community have experienced similar issues! Would you mind sharing screenshots of your issue or a share link to the level in question. This will allow me to get to the root of your exact problem.

Something to keep in mind is using a single Scanner object, the methods nextInt( ), nextDouble( ), next( ), and nextLine( ) may be used in any sequence with the following exception:
It is not permissible to follow nextInt( ) or nextDouble( ) with nextLine( ).

If you want to know why, this is a pretty good resource describing the reason behind the issue.

One way I have mitigated this issue is bu creating a new Scanner object for use with nextLine( ) and any subsequent inputs.

Hope this helps and please reach out again with your specific link so we can assist with more detail.

Best,
Sam

1 Like

That is interesting because if I use an IDE such as JCreator or JGrasp then one Scanner object can be used without an issue. I think the issue is with code.org’s IDE. I will instruct the student to create more Scanner objects.

@mjdeister - Hi Myra, Java Studio will also work with a single Scanner object, but as Sam pointed out, problems occur if you call to use nextLine() after called a nextInt() or a nextDouble(). This issue also occurs in other IDE’s like JCreator and JGrasp. This because Java will capture the CRLF character and allow the call to nextLine() to capture it. This is why it is suggested that Scanner objects that read numerical data be different from those that read text. I was taught to do that when I started programming in Java.
Sylvia

1 Like

That is interesting because I have never run across this issue before.

Thank you for bring this up on the forum, Myra.

If you have screenshots or links to code that behaves differently on JavaLab versus other IDE’s, that could help the curriculum team and engineers to check more in depth with what is going on. Sharing here on the forum helps the teacher community to be informed of the issue, and to provide ideas and work arounds. Emailing support@code.org will get the issue directly to the Code.org team if there is a bug.

Best,
Lindsay