Scanner Class In Java

There are various methods in java to take input from the keyboard one of the method is Scanner Class. The scanner class comes under the java.util package.

There are some important methods in Scanner class:

  1. nextInt() – This method is used to read an integer value from the keyboard.
  2. nextFloat() – It is used to read a float value.
  3. nextLong() – It is used to read a long value.
  4. next() – It is used to read a string.
import java.util.Scanner;
class Add
{
Scanner sc=new Scanner(System.in);
void add()
{
System.out.println("enter first number");
int a=sc.nextInt();
System.out.println("enter second number");
 int b=sc.nextInt();

 int c=a+b;
System.out.println(+c);
}

public static void main(String args[])
{
Add a1=new Add();
a1.add();
}
}

Note: The scanner class does not support nextChar() to input a character.

  • Scanner sc=new Scanner(System.in);
  • Char ch=sc.next().charAt(0);

Leave a Reply