Example & Tutorial understanding programming in easy ways.

How many ways can we read data from the keyboard?.

There are many ways to read data from the keyboard. These are:

InputStreamReader class:

InputStreamReader class can be used to read data from keyboard.

BufferedReader class:

BufferedReader class can be used to read data line by line by readLine() method.

Console class (I/O)

The Console class can be used to get input from the keyboard.

Scanner class:

There are various ways to read input from the keyboard, the java.util.Scanner class is one of them. The Scanner class breaks the input into tokens using a delimiter which is whitespace bydefault. It provides many methods to read and parse various primitive values.

InputStreamReader and BufferdReader class:

Example of reading data from keyboard by InputStreamReader and BufferdReader class

In this example, we are connecting the BufferedReader stream with the InputStreamReader stream for reading the line by line data from the keyboard,listed below..

import java.io.*;

class test{

public static void main(String args[])throws Exception{

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("Enter your name");

String name=br.readLine();

System.out.println("Welcome "+name);

} }

Console class (I/O):

In this example, we make a class using console that reads the name of the user and prints it.
 

import java.io.*;

class Consoletest

{

public static void main(String args[])

{

Console con=System.console();

System.out.println("What is your name");

String str=con.readLine();

System.out.println("Welcome " +str+ " to the java");

}}


Scanner class:
 

import java.util.Scanner;

class Scannertest

{

public static void main(String args[])

{

Scanner scn=new Scanner(System.in);

System.out.println("Whats your rollno");

int rnum=scn.nextInt();

System.out.println("Whats your name");

String sname=scn.next();

System.out.println("Your Rollno:" + rnum+ " name:" + sname);

}}

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!