Now we can discuss how can input to the user in Java. Java can be provide input to the user mainly used the following two classes.
import java.util.Scanner;
class Test
{
String name;
int rollno;
int hindi,eng;
int total;
float per;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name");
String name=sc.next();
System.out.println("enter rollno");
rollno=sc.nextInt();
System.out.println("enter marks hindi");
hindi=sc.nextInt();
System.out.println("enter marks eng");
eng=sc.nextInt();
total=hindi+ eng;
per=(total*100)/200;
System.out.println("total="+total) ;
System.out.println("per="+ per) ;
}
}
class Test1
{
public static void main(String args[])
{
Test s=new Test();
s.getdata();
}
}
- Scanner clas
- Buffer reader class
In Java programs mainly used by Scanner class because it is very simple and easy to understand. It does not require any types constructors.
Example : before declare a class we can import the Scanner class becabecaue scanner class inside the until package in the Java, without importanting we can not use scanner class. Then we have declared Test class.
import java.util.Scanner;
class Test
{
String name;
int rollno;
int hindi,eng;
int total;
float per;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name");
String name=sc.next();
System.out.println("enter rollno");
rollno=sc.nextInt();
System.out.println("enter marks hindi");
hindi=sc.nextInt();
System.out.println("enter marks eng");
eng=sc.nextInt();
total=hindi+ eng;
per=(total*100)/200;
System.out.println("total="+total) ;
System.out.println("per="+ per) ;
}
}
class Test1
{
public static void main(String args[])
{
Test s=new Test();
s.getdata();
}
}
