Using the scanner class in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Using the scanner class in Java?

I want to put user data into my Java program but I don't know how to make it work. I have the layout of the code but I just can't seem to get the user data to work. The code that I am using is just like guess my number, I want the user to enter in some number and for my program to calculate how much the user was off by. I got the import java.util.Scanner; down as well as the Scanner scan = new Scanner (; But how to I get the program to take the data and calculate it? Thanks!

28th Mar 2017, 12:48 AM
PaperGami T.
PaperGami T. - avatar
2 Answers
+ 7
nextInt() for int nextDouble() for double next() for string (one word)(no space)(will just take the word before the first space) nextLine() string (whole line)(sentence)(until enter)
28th Mar 2017, 2:51 AM
Heng Jun Xi
Heng Jun Xi - avatar
+ 5
int userInput = scan.nextInt(); You can use the next(); methods of the scanner. Theres also other stuff like nextLine() to grab the whole line of input (used for Strings).
28th Mar 2017, 2:44 AM
Rrestoring faith
Rrestoring faith - avatar