Scanner
class in Java is used to take user input of primitive data types like int
, double
, etc., and strings.
Scanner
class is present in the java.util
package.
public final class Scanner implements Iterator, Closeable
{
// methods
}
Scanner
Class:
Scanner
class with examples:
Method | Description | Example | Output |
---|---|---|---|
nextInt() |
Reads the next token as an int . |
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(n); |
Depends on input (e.g., 10 ) |
nextDouble() |
Reads the next token as a double . |
Scanner sc = new Scanner(System.in); double d = sc.nextDouble(); System.out.println(d); |
Depends on input (e.g., 5.5 ) |
nextLine() |
Reads the entire line including spaces until newline. |
Scanner sc = new Scanner(System.in); String line = sc.nextLine(); System.out.println(line); |
Depends on input (e.g., Hello World ) |
next() |
Reads the next token (word) from input. |
Scanner sc = new Scanner(System.in); String word = sc.next(); System.out.println(word); |
Depends on input (e.g., Hello ) |
hasNext() |
Checks if there is another token available. |
Scanner sc = new Scanner(System.in); while(sc.hasNext()){ System.out.println(sc.next()); } |
Prints all tokens one by one |
hasNextInt() |
Checks if the next token can be interpreted as an int . |
Scanner sc = new Scanner(System.in); if(sc.hasNextInt()){ int n = sc.nextInt(); System.out.println(n); } |
Depends on input |
useDelimiter(String pattern) |
Sets the delimiter pattern used to separate tokens. |
Scanner sc = new Scanner("Java,Python,C++"); sc.useDelimiter(","); while(sc.hasNext()){ System.out.println(sc.next()); } |
Java Python C++
|
close() |
Closes the scanner and releases resources. |
Scanner sc = new Scanner(System.in); sc.close(); |
Scanner is closed |
import java.util.Scanner;
public class ScannerExample
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name : ");
String name = sc.nextLine();
System.out.print("Enter your age : ");
int age = sc.nextInt();
System.out.print("Enter your salary : ");
double salary = sc.nextDouble();
System.out.println("--------------------------------");
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Salary : Rs. " + salary);
sc.close();
}
}
Enter your name : Deepak Enter your age : 30 Enter your salary : 200000 -------------------------------- Name : Deepak Age : 30 Salary : Rs. 200000.0
System.in
to read input from the keyboard.
If we want to read from a file, we can pass a File object instead.
nextXYZ()
. For example:
nextInt()
method.
nextShort()
method.
nextLine()
method.
import java.util.Scanner;
public class MainApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a line:");
String line = sc.nextLine();
System.out.println("You entered: " + line);
sc.close();
}
}
next().charAt(0)
.
Scanner sc = new Scanner(System.in);
char ch = sc.next().charAt(0);
Scanner(File file)
constructor.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class ReadFile
{
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("input.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println(line);
}
sc.close();
}
}
Scanner
object after use to avoid resource leaks.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.