Experiment No.
2
Roll No
Aim: Program on accepting input through keyboard
a. Write a program to create a class Student with data ‘name, city and age’ along with method printData
to display the data. Create the two objects s1 ,s2 to declare and access the values.
//Accept input using scanner and bufferreader classes
import [Link].*;
import [Link];
public class InputExample
{
public static void main(String[] args)throws IOException
{
//Using scanner for input
Scanner scanner=new Scanner([Link]);
[Link]("Enter your name:");
String name=[Link]();
[Link]("Enter your age:");
int age=[Link]();
//Using BufferedReader for input
BufferedReader reader=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter your city:");
String city=[Link]();
//Displaying the inputs
[Link]("\n--User Details--");
[Link]("Name:"+name);
[Link]("Age:"+age);
[Link]("City:"+city);
}
}
Output:
Conclusion
Successfully implemented this program.