0% found this document useful (0 votes)
15 views5 pages

Java User Input with Scanner Class

The document explains how to take user input in Java using the Scanner class, which is part of the java.util package and was introduced in Java 5. It provides step-by-step instructions on how to import the Scanner class, create a Scanner object, and use its methods to read different data types. Additionally, it compares Scanner with BufferedReader, highlighting their respective advantages and use cases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Java User Input with Scanner Class

The document explains how to take user input in Java using the Scanner class, which is part of the java.util package and was introduced in Java 5. It provides step-by-step instructions on how to import the Scanner class, create a Scanner object, and use its methods to read different data types. Additionally, it compares Scanner with BufferedReader, highlighting their respective advantages and use cases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java User Input

The most common way to take user input in Java is using Scanner class
which is part of [Link] package. The scanner class can read input from
various sources like console, files or streams. This class was introduced
in Java 5. Before that we use BufferedReader class(Introduced in Java
1.1). As a beginner, we will suggest to use Scanner class.

Follow these steps to take user input using Scanner class:


 Import the Scanner class using import [Link];
 Create the Scanner object and connect Scanner
with [Link] by passing it as an argument i.e. Scanner scn =
new Scanner([Link]);
 Print a message to prompt for user input and you can use the
various methods of Scanner class like nextInt(), nextLine(), next(),
nextDouble etc according to your need.
Example 1: Here, we are taking integer inputs from the user.
// taking user input
import [Link];
class Geeks
{
public static void main(String[] args)

{
// Creating Scanner class object
Scanner scn = new Scanner([Link]);
// Enter first input
[Link]("Enter First Number: ");
int a = [Link]();
[Link]("Enter Second Number: ");
int b = [Link]();
[Link]("Sum: " + (a + b));
// Closing the scanner to release resources
[Link]();
}
}
Output:
Enter First Number: 2
Enter Second Number: 3
Sum: 5
Scanner class provides some methods to read different data
types:
Method Description

Used for reading Boolean value.


nextBoolean()
Method Description

nextByte() Used for reading Byte value.

nextDouble() Used for reading Double value.

nextFloat() Used for reading Float value.

nextInt() Used for reading Int value.

nextLine() Used for reading Line value.

nextLong() Used for reading Long value.

nextShort() Used for reading Short value.

Example 2: Here, we are taking multiple user inputs like string, float,
double etc. and using the above mentioned methods of Scanner Class.
// taking multiple user input using Scanner Class
import [Link];

class Geeks
{
public static void main(String[] args)
{
// Scanner definition
Scanner scn = new Scanner([Link]);

// input is a string(one word)


// read by next() method
String str1 = [Link]();

// print string
[Link]("Entered String str1: " + str1);

// input is a string(complete Sentence)


// read by nextLine() method
String str2 = [Link]();

// print string
[Link]("Entered String str2: " + str2);

// input is an integer
// read by nextInt() method
int x = [Link]();

// print integer
[Link]("Entered Integer: " + x);

// input is a floatingValue
// read by nextFloat() method
float f = [Link]();

// print floating value


[Link]("Entered FloatValue: " + f);
}
}
Output:
Entered String str1 : Geeks
Entered String str2 : Geeks For Geeks
Entered Integer : 123
Entered FloatValue : 123.090
Another method to take user input is using BufferedReader Class. It is a
simple class that is used to read a sequence of characters. It provides
several methods, including:
 read(): Reads a single character.
 read(char[] cbuf): Reads an array of characters.
 readLine(): Reads an entire line of text.
To know more about BufferedReader Class, please refer to the article –
BufferedReader Class in Java
BufferedReader vs Scanner Class
Aspects BufferedReader Scanner

Primary Efficient reading of Reading formatted input (e.g.,


Use character streams. integers, strings).

Slower due to parsing


Faster for large input as
Speed overhead
it does less parsing.
(e.g., nextInt(), nextFloat()).

Throws checked
Exception No checked exceptions; easier
exceptions
Handling to use.
(e.g., IOException).

Allows reading larger Best suited for reading simple


Flexibility
input efficiently. data types.

Thread Synchronized, making Not thread-safe by default.


Aspects BufferedReader Scanner

Safety it thread-safe.

Common Used for reading large Commonly used for smaller,


Use input efficiently. formatted input.

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 [Link];

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);

[Link]("Enter name, age and salary:");

// String input
String name = [Link]();

// Numerical input
int age = [Link]();
double salary = [Link]();

// Output input by user


[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Salary: " + salary);
}
}

FAQs – Java User Input


What are the different ways to take input in Java?
Methods to take input in Java as mentioned below:
 BufferedReader
 Scanner
 Console

You might also like