All for Joomla All for Webmasters

Java Scanner class

There are various ways to read input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter that is whitespace bydefault. It provides many methods to read and parse various primitive values.

Java Scanner class is widely used to parse text for string and primitive types using regular expression.

Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

Commonly used methods of Scanner class

There is a list of commonly used Scanner class methods:

Method Description
public String next() it returns the next token from the scanner.
public String nextLine() it moves the scanner position to the next line and returns the value as a string.
public byte nextByte() it scans the next token as a byte.
public short nextShort() it scans the next token as a short value.
public int nextInt() it scans the next token as an int value.
public long nextLong() it scans the next token as a long value.
public float nextFloat() it scans the next token as a float value.
public double nextDouble() it scans the next token as a double value.

Java Scanner Example to get input from console

Let’s see the simple example of the Java Scanner class which reads the int, string and double value as an input:

  1. import java.util.Scanner;
  2. class ScannerTest{
  3.  public static void main(String args[]){
  4.    Scanner sc=new Scanner(System.in);
  5.    System.out.println(“Enter your rollno”);
  6.    int rollno=sc.nextInt();
  7.    System.out.println(“Enter your name”);
  8.    String name=sc.next();
  9.    System.out.println(“Enter your fee”);
  10.    double fee=sc.nextDouble();
  11.    System.out.println(“Rollno:”+rollno+” name:”+name+” fee:”+fee);
  12.    sc.close();
  13.  }
  14. }

Output:

       Enter your rollno
       111
       Enter your name
       Ratan
       Enter 
       450000
       Rollno:111 name:Ratan fee:450000

Java Scanner Example with delimiter

Let’s see the example of Scanner class with delimiter. The \s represents whitespace.

  1. import java.util.*;
  2. public class ScannerTest2{
  3. public static void main(String args[]){
  4.      String input = “10 tea 20 coffee 30 tea buiscuits”;
  5.      Scanner s = new Scanner(input).useDelimiter(“\\s”);
  6.      System.out.println(s.nextInt());
  7.      System.out.println(s.next());
  8.      System.out.println(s.nextInt());
  9.      System.out.println(s.next());
  10.      s.close();
  11. }}

Output:

10
tea
20
coffee
PinIt
submit to reddit

Leave a Reply

Top