Tuesday, 25 November 2014

Hey friends, welcome to simplyjavapro,


Well Its been quiet a lot of time since you've had known about Data types in Java.


So, lets get stated and try to do some really stuff in Java Programming.

Have you ever wondered how does the computer access that you "Input" and process it to give you a desired Output. Well, it is nothing but a set of codes, which make it possible.

In a powerful programming language, such as Java, you are expected to "import" the ability to use the Input/Output devices.


The first thing that you need to do in java to enable I/O(input-output) is to the following line of code before you mention your class.

import java.io.*;

The statement above imports a package called "io" from the java library which enables you to do input and output stuff in your programs.

The next step is obviously to mention your class, and then the main method.

class InputOutputExample
{

        public static void main(String args[]) throws IOException
       {
                 //Block of code              
        }
}
The code "throws IOException" takes care for any abnormalities that may occur during an IO.

Notice the double slashes used inside the main method. These indicate single line comments in Java.We shall discuss more about comments later. For now, you may consider that the line written in such a manner is not a part of the executable code.

In place of the Commented section you've got to use a few Classes to enable standard I/O in your program.

Write the following line of code:

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);


The above two lines allow you to take input from the standard input device, the keyboard (thanks to System.in)


Now take input using the readLine() method of the BufferedReader class.

Write the following line of code:

String input=br.readLine();


and this is it....

You have got your input from the keyboard. Now you could use it whichever way you like, print it perhaps.

System.out.println(input);

now just close your main methods and your class. Compile and run your code.

During the interpretation of the code(or during the time you program is running) your programs halts to take the input, type in any string and press ENTER.

and there you have your output.


No comments:

Post a Comment