Console input output
console input output by buffercode

Hello Fiends
Today we learn about the concept of handling console input or output by using java code. Here we refer console to command prompt (cmd) . Friend we can easily read characters or line entered on console by using just a java code. Reading console input or output using java code is done by using BufferedReader.
To read or write from console we have to first obtain character based stream by using which console input and outputs are easily handled. To obtain character based stream we have to firstly enclose System.in in the object of BufferedReader. The object of Buffered reader can easily handle the console input by using read() or readLine() which is used to Reading Console input using java codes.
To start Coding we have to check:
1) Is java devolvement kit (jdk) is installed on your pc? If not then please install it first.
2) Is path variable set for java?
Now we can start Coding
Open Notepad And Start Coding
JAVA CODES:
import java.io.*;
import java.util.*;
class r
{
public static void main(String args[])
{
try
{
int ch;
Scanner sc=new Scanner(System.in);
System.out.println(“************Reading From Console By Praveen Kumar**********”);
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“******MENU******”);
System.out.println(“1.Read Line From Console”);
System.out.println(“2.Read Character From Console”);
System.out.println(“Press 1 or 2 for choice locking”);
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println(“Enter some lines”);
System.out.println(“YOU ENTERED : “+b.readLine());
break;
case 2:
System.out.println(“Enter Some character”);
System.out.println(“YOU ENTERED : “+cl(char)b.read());
break;
default :
System.out.println(“wrong choice”);
break;
}
}
catch(IOException e)
{
System.out.println(“Exeption Handled”);
System.out.println(e);
}
}
}

Now follow these steps
1) Save file as r.java.
2) Compile it by cmd as :- javac r.java
3) Run it as :- java r
4) After which your output will come
:::::::::::::::::::::::::::::::::: Explanation Of Code:::::::::::::::::::::::::::::::::::::
1) BufferedReader is used to read character based stream which is entered on console.
2) System.in is attached to BufferedReader to make character based stream.
3) Try, catch blocks are used to handle exception.
4) read() function is used to read character entered by user.
5) readLine() function is used to read line or string entered by user.

**********************************OUTPUT**********************************

Console

For further help click on following links

1)For programming click here.

2) For Query click here.

Thanks for reading the post ……by Buffercode

NO COMMENTS