Skip to content
Advertisement

When having String and int as an output – how do I print string first?

This code doesn’t work, whenever I as an user input String data first and then int data, it just accepts input and doesn’t print out the data. If I change:

JavaScript

position of these two code blocks, and enter int first and String as second value after then it happily prints first int number and then String. Please help out how to solve it. I want to be able Name and Family name first and then I want to have age.

JavaScript

My input is:

Aks Eyeless 2022

Advertisement

Answer

Here’s how to enter the name and age on one line.

JavaScript

You perform a Scanner nextLine method, and then you parse (separate) the age from the name. The String class has lots of useful methods for parsing a string.

Here’s the modified code.

JavaScript
Advertisement