USING SCANNER CLASS TO INPUT
BY USING THIS CLASS WE CAN DIRECTLY CONNECT WITH KEYBOARD. THE MAIN ADVANTAGE OF USING THIS CLASS IS TO READ NUMERIC TYPES OF DATA WITHOUT CONVERTING IT. IT IS AVAILABLE IN java.util PACKAGE. THIS CLASS HAS THE FOLLOWING METHODS :
next( ), nextLine( ), nextByte( ), nextShort( ), nextInt( ),nextFloat( ), nextDouble( ) etc.
Example of using Scanner to a normal results of student program:
import java.io.PrintStream;
import java.util.Scanner;
class Program // Program is a Class word and 'P' should be in capital form
{
public static void main(String args[])
{
PrintStream ps = new PrintStream(System.out);
Scanner sc = new Scanner(System.in);
int sno,em,tm,hm,mm,scm,som,tot;
float avg;
String sname,res;
ps.println("Enter Student Number and Name");
sno = sc.nextInt( );
sname = sc.next( );
ps.println("Enter English, Telugu and Hindi Marks");
em = sc.nextInt( );
tm = sc.nextInt( );
hm = sc.nextInt( );
ps.println("Enter Maths, Science and Social Marks");
mm = sc.nextInt( );
scm = sc.nextInt( );
som = sc.nextInt( );
tot = em + tm + hm + mm + scm + som;
avg = tot / 6.0F;
if(em >= 34 && tm >= 34 && hm >= 34 && mm >= 34 && scm >= 34 && som >= 34 && avg >= 80)
{
res = "Distinction";
}
else
if(em >= 34 && tm >= 34 && hm >= 34 && mm >= 34 && scm >= 34 && som >= 34 && avg >= 60)
{
res = "First Class";
}
else
if(em >= 34 && tm >= 34 && hm >= 34 && mm >= 34 && scm >= 34 && som >= 34 && avg >= 50)
{
res = "Second Class";
}
else
if(em >= 34 && tm >= 34 && hm >= 34 && mm >= 34 && scm >= 34 && som >= 34 && avg >= 40)
{
res = "Third Class";
}
else
if(em >= 34 && tm >= 34 && hm >= 34 && mm >= 34 && scm >= 34 && som >= 34 && avg >= 34)
{
res = "Ordinary Pass";
}
else
{
res = "Fail";
}
ps.println("\t\t Abc School");
ps.println("\t\t=================");
ps.println("Student Number : " + sno);
ps.println("Student Name : " + sname);
ps.println("Total Marks : " + tot);
ps.printf("Average Marks : %.3f\n",avg);
ps.print("Result : " + res);
}
}
RESULT:
Enter Student Number and Name
2
JOHN
Enter English, Telugu and Hindi Marks
60
70
87
Enter Maths, Science and Social Marks
90
87
75
Abc School
=================
Student Number : 2
Student Name : JOHN
Total Marks : 469
Average Marks : 78.167
Result : First Class