WHAT IS SCANNER?

The Scanner:-

A Scanner object can parse user input entered on the console or from a file. A Scanner breaks its input into separate tokens (which are typically separated by white space), and then returns them one at a
time. The scanner provides methods to convert the tokens into values of different types. For example,this code reads two numbers from the console and prints their sum:

Scanner sa=new Scanner(System.in);
int a=sa.nextInt();
int b=sa.nextInt();
int c=a+b;

Creating Scanners:-
Whenever using scanners, be sure to include the proper import line:
import java.util.Scanner;
We will create scanners in two ways:
1. To read from the console, use the following:
Scanner input = new Scanner(System.in);
2. To read from a file, use the following:
Scanner input = new Scanner(new FileStream(“filename.txt”));

Scanner Methods:-

Method Computes

nextBoolean() reads and converts next token to a boolean value
nextInt() reads and converts next token to a integer value
nextLong() reads and converts next token to a long value
nextDouble() reads and converts next token to a double value
nextString() or next() reads next token and returns it as a String
nextLine() reads until the next new line and returns a String
hasNextBoolean() returns true iff the next token is either “true” or “false”
hasNextInt() returns true iff the next token is an integer
hasNextLong() returns true iff the next token is a long
hasNextDouble() returns true iff the next token is a real number
hasNextString() or hasNext() returns true iff there is at least one more token of input
hasNextLine() returns true iff there is another line of input

Example:-
Code is given below:-

import java.util.*;
import java.util.Scanner;

public class ScannerTest
{
int a,b;

public int Add()
{
Scanner sa=new Scanner(System.in);
int a=sa.nextInt();
int b=sa.nextInt();
int c=a+b;
return c;
}
public int Sub()
{
Scanner sa=new Scanner(System.in);
int a=sa.nextInt();
int b=sa.nextInt();
int c=a-b;
return c;
}
public int Mul()
{
Scanner sa=new Scanner(System.in);
int a=sa.nextInt();
int b=sa.nextInt();
int c=a*b;
return c;
}
public double Div()
{
Scanner sa=new Scanner(System.in);
double a=sa.nextInt();
double b=sa.nextInt();
double c=a/b;
return c;
}
public double Sqroot()
{
Scanner sa=new Scanner(System.in);
double a=sa.nextInt();
double b=sa.nextInt();
double c=Math.sqrt(a*a+b*b);
return c ;
}
public static void main(String[] args)
{
ScannerTest st=new ScannerTest();
System.out.println(“Enter the number for Addition:-“);
System.out.println(“Total:-“+st.Add());
System.out.println(“Enter the number for Subtraction:-“);
System.out.println(“Total:-“+st.Sub());
System.out.println(“Enter the number for Multiplication:-“);
System.out.println(“Total:-“+st.Mul());
System.out.println(“Enter the number for Division:-“);
System.out.println(“Total:-“+st.Div());
System.out.println(“Enter the number for Squareroot:-“);
System.out.println(“Total:-“+st.Sqroot());
}
}

Output is given below:-
Enter the number for Addition:-
12
23
Total:-35
Enter the number for Subtraction:-
34
12
Total:-22
Enter the number for Multiplication:-
12
12
Total:-144
Enter the number for Division:-
12
7
Total:-1.7142857142857142
Enter the number for Squareroot:-
5
5
Total:-7.0710678118654755

Advertisements

About pinku12

HIMANSHU SRIVASTAVA VARANASI
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s