Search⌘ K
AI Features

Strings and the Class Scanner

Learn how to use Java's Scanner class to read complete lines, individual words, and other tokens as strings. Understand how nextLine and next work, how whitespace separates tokens, and how custom delimiters let you parse keyboard input or strings created in your program.

We introduced the standard class Scanner in the lesson Simple Input from the Keyboard. At that time, we learned how to use it to input numbers typed at the keyboard into our program. In particular, we saw and used the Scanner methods nextInt and nextDouble. It might surprise you to learn that all input to a program, including numeric data, is read as a string. Methods such as nextInt and nextDouble convert the strings they ...