Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+17 votes

How can I get the user type a line of input with many integer scores?

asked in CSC211_Winter2018 by (1 point)

2 Answers

+13 votes

Nick is exactly correct about the concept of tokens.

I would use console.next() to read the name token because console.next() returns a String. If you use console.next() to read an integer, the integer will be returned as a String ("5") not the integer 5.

Use console.next() to read the name, but console.nextInt() to read the first integer (representing the number of scores) and a loop (as Nick suggests) to read the remaining integer scores with console.nextInt().

answered by (1 point)
+11 votes

Recall that a token is a unit of user input, as read by the Scanner. Tokens are separated by whitespace, such as a space, a tab, or a new line.

Because each integer is separated by whitespace, you can use a for loop to run through a line of user input a specified number of times and utilize console.next() to have your program run through the loop with each token.

answered by (1 point)