0% found this document useful (0 votes)
149 views

Java User Input

The Scanner class in the java.util package is used to get user input in Java programs. It provides various methods like nextLine(), nextInt(), and nextDouble() to read different data types from the user. For example, nextLine() reads a String, nextInt() reads an integer, and nextDouble() reads a double. A Scanner object is created passing System.in, and methods like nextLine() are then used to get input and save it to variables for use in the program.

Uploaded by

Margerie Fruelda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Java User Input

The Scanner class in the java.util package is used to get user input in Java programs. It provides various methods like nextLine(), nextInt(), and nextDouble() to read different data types from the user. For example, nextLine() reads a String, nextInt() reads an integer, and nextDouble() reads a double. A Scanner object is created passing System.in, and methods like nextLine() are then used to get input and save it to variables for use in the program.

Uploaded by

Margerie Fruelda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java User Input

The Scanner class is used to get user input, and it is found in


the java.util package.

To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation. In our example,
we will use the nextLine() method, which is used to read Strings:

Example
import java.util.Scanner; // Import the Scanner class

class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter username");

String userName = myObj.nextLine(); // Read user input


System.out.println("Username is: " + userName); // Output user
input
}
}
Run example »

If you don't know what a package is, read our Java Packages Tutorial.

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

In the example below, we use different methods to read data of various types:

Example
import java.util.Scanner;

class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);

System.out.println("Enter name, age and salary");


// String input
String name = myObj.nextLine();

// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();

// Output input by user


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

You might also like