Thursday, 14 July 2011

Standard Input




Standard Input:-
For using this we import io package which is present in java package.
So program looks like
import java.io.*;
class StandardIP
{
Int sn,fn,ans;
public static void main(String[] arg)
throws IOException                       // used to throw exception caused bybu
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//or
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new Buffered(isr);
System.out.println(“Enter the First number”);
int fn=Integer.parseInt(br.readLine());
System.out.println(“Enter the second number”);
ant sn=Integer.parseInt(br.readLine());
ans=sn+fn;
System.out.println(“ans=”+ans);
}
}
Note:-
For the standard input we need a function that is readLine() it is available in BufferedReader class and BufferedReader class is available in java.io package so we need to import java.io package only then we can create object of BufferedReader class through which we will call readLine() function.While creating objects of BufferedReader we have to pass object of InputStreamReader that is treated with the help of System class and of type InputStream.

Note:-
throws IOException is used to throw exception caused by readLine().

Note:-All the variables inside the function should be initialised because they are local variables and are not auto-initialised.



Note:-Java performs bound checking of array.
In java we will get exception named array out of range if we give input greater than bounds of array.

No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013