Last modified: January 09, 2024
In CodeHS, you got user input via readLine
, readInt
, etc.
In Java, you can get user input via the Scanner class.
Example:
// import the Scanner class so we can use it in our program
import java.util.Scanner;
class Demo {
public static void main(String[] args){
// Declare a Scanner object named "input"
Scanner input;
/* Instantiate (create) the Scanner object. Notice that we are passing
System.in into the constructor's parameter list. System.in is an InputStream
object that stores input typed in to the console. When a scanner is initalized
with an instance of an InputStream, it can read the data stored in that
InputStream (in this case whatever the user types into the console). */
input = new Scanner(System.in);
// Declare two variables, a String and integer
String name;
int age;
// Prompt the user for input, then get the input
System.out.print("Enter your name: ");
name = input.next();
System.out.print("Enter your age: ");
age = input.nextInt();
// Print the results
System.out.println("Hello there, " + name +
"! In ten years you will be " + (age + 10));
}
}
}
Keep in mind that:
In order to use the Scanner class, it must be imported
The Scanner class has different methods for inputting data. Ex: next() vs nextInt()
When printing the prompt "Enter your name: ", instead of System.out.println(), we used System.out.print() so the user's input appears on the same line as the prompt.
Java expressions that are too long to fit on one line can split into multiple lines as long as an operator separates the lines. Ex:
"hello " +
"there"
The Scanner class is designed to read tokens. Tokens are separated by white space (space, tab, new line ...etc). For example, in "Hi, I am 26 years old.", there are 6 tokens: "Hi,", "I", "am", "26", "years" and "old.". Note that the comma is included in the first token because there is no white space between Hi and the comma. The period is included in the last token "old." for the same reason.
Here is a list of methods for Scanner that you will most likely use:
Method Name | Return Type | Purpose |
---|---|---|
next() | String | Finds and returns the next complete token from this scanner |
nextBoolean() | boolean | Scans the next token of the input and returns a boolean value based on whether the token is "true" or "false". Throws an exception if the token is not "true" or "false" |
nextDouble() | double | Scans the next token of the input and returns a double parsed from the token. Will throw exception if the token is not a valid number |
nextInt() | int | Scans the next token of the input and returns an int parsed from the token. Will throw exception if the token is not a valid integer |
nextLine() | String | Advances this scanner past the current line and returns the input that was skipped |
Every built-in Java class comes with API (Application Programming Interface) documentation. API documentation is an owner's manual on how to use a java class written by someone else. Here is a simplified version of the Scanner API with comments in green:
You can find the documentation for classes online by searching for Java ClassName API. For example searching for "Java Scanner API" should lead you to the Scanner API.