What is Scanner Class in Java

2
This class exist under java.util package ,It is used to manage User Input Operation Under Console Screen.
hence it is also called input manager class in Java.
To take input from user's ,java provide System.in reference which will be handled by Scanner Class.
Scanner sc = new Scanner(System.in);
It will manage System.in input under Scanner  Object ,in above Statement sc is the Scanner Object.
After that we can type Scanner Object to other datatype.
int a = sc.nextInt();
float b = sc.nextFloat();
double d = sc.nextDouble();
String s = sc.next();
char ch = sc.next().charAt(0);
...............................................................................................................................................

import java.util.Scanner;
class SI
{
   public static void main(String args[])
   {
      Scanner sc = new Scanner(System.in);
      float p=12000,r=2,t=2,si;
      System.out.println("enter value for p");
      p=sc.nextFloat();
      System.out.println("enter value for r");
      r=sc.nextFloat();
      System.out.println("enter value for t");
      t=sc.nextFloat();
      si=(p*r*t)/100;
      System.out.println("Result is "+si);
      }
Tags

Post a Comment

2Comments

POST Answer of Questions and ASK to Doubt

  1. Sandeep kelwa

    class SI
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    float p=12000,r=2,t=2,si;
    System.out.println("enter value for p");
    p=sc.nextFloat();
    System.out.println("enter value for r");
    r=sc.nextFloat();
    System.out.println("enter value for t");
    t=sc.nextFloat();
    si=(p*r*t)/100;
    System.out.println("Result is "+si);


    }


    }

    ReplyDelete
  2. import java.util.Scanner;
    class UserDetail
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your Name-");
    String name=sc.nextLine();
    System.out.println("Enter your Age -");
    int age=sc.nextInt();
    System.out.println("Enter your Phone No.-");
    long phoneNo=sc.nextLong();
    System.out.println("Enter your Gender-");
    char gender=sc.next().charAt(0);
    System.out.println("Enter your Hight-");
    float hight=sc.nextFloat();
    System.out.println("..................");
    System.out.println("Name"+name);
    System.out.println("Age"+age);
    System.out.println("Phone No."+phoneNo);
    System.out.println("Gender"+gender);
    System.out.println("Hight"+hight);
    }
    }

    ReplyDelete
Post a Comment