Vote Up 0 Vote Down

How to use standard input and output in java?

How to use standard input and output in java? Use typical examples and links..
flag

Answers


Vote Up 0 Vote Down
There are many ways. The one that I prefer is using the java.io.BufferedReader class.

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine(); // here line stores the first line that you enter in the console.
flag | link |
Vote Up 0 Vote Down
Use

 import java.util.Scanner;
Scanner sn = new Scanner(System.in);
int n=sn.nextInt();
//it works just like cin in c++
flag | link |
Vote Up 0 Vote Down
There are different ways but i use only these two .To simply write to a console use

System.out.println("hello"); 


or you can use PrintWriter Class

 PrintWriter writ=new PrintWriter(System.out); 


followed by

writ.println("hello"); 


and finally

writ.close();


Though PrintWriter class is tedious in case you want to write to console but is very useful if you want to do file I/O .And finally don't forget the import statement

import java.io.*;
flag | link |

Your Answer

Login before answering

Login with facebook