User Input
christian.dinh2476 total contributions
Published Aug 4, 2021Updated Sep 9, 2021
Contribute to Docs
The Scanner
class is used to get user input, and it is found in the java.util
package.
To use the Scanner
class:
- Import the
Scanner
class at the top of the file. - Create a
Scanner
object. - Use a method from the
Scanner
class.
To import the Scanner
class, add at the top of the file:
import java.util.Scanner;
In our example, we will use the .nextLine()
method, which is used to read Strings
:
import java.util.Scanner; // 1. Import the Scanner classclass Main {public static void main(String[] args) {Scanner myObj = new Scanner(System.in); // 2. Create a Scanner objectSystem.out.println("Enter your user name");String userName = myObj.nextLine(); // 3. Read the user input with .nextLine()System.out.println("The username is: " + userName);}}
Here, the user can enter some text in the terminal, press enter, and that string will get stored in the variable userName
.
Input Types
In the example above, we used the .nextLine()
method, which is used to read Strings
. To read other types, look at the table below:
Method | Description |
---|---|
.nextBoolean() |
Reads a boolean value from the user |
.nextByte() |
Reads a byte value from the user |
.nextDouble() |
Reads a double value from the user |
.nextFloat() |
Reads a float value from the user |
.nextInt() |
Reads a int value from the user |
.nextLine() |
Reads a String value from the user |
.nextLong() |
Reads a long value from the user |
.nextShort() |
Reads a short value from the user |
All contributors
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.