+ 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 (System.in); But how to I get the program to take the data and calculate it? Thanks!
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)
+ 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).