Coder's Guild Mailing List

Re: Java Console IO

Posted by Benjamin Johnston on 2000-04-11

> Hi,
> 
> I am new to Java. What is the equivalent of the following in Java:
> 
> o	cout << anInteger
System.out.println(anInteger);

> o	cout << aCharacter
System.out.println(aCharacter);

> o	cin >> anInteger
First you have to wrap the input stream in a BufferedReader:
BuffferedReader br = new BufferedReader(new InputStreamReader(System.in));

ie. System.in is the console input stream... wrapping it in an
InputStreamReader turns it into a reader, which BufferedReader takes, and
adds functionality to.

Then,
anInteger = Integer.parseInt(br.readLine().trim());

(the trim() removes any whitespace on the read line).

> o	someCharacter = getch()
This, you can't really do... the console input stream only takes data when
you press enter (and then it all comes "into" the system at once).

> Please do help me out. An assignment is due tomorrow. Its a 
> linked list assignment - I have done all other things (ie the functions 
> are working when called with proper parameters) but user input is 
> not working. 
> 
> I have tried System.in.read and DataInputStream.readInt, but have 
> failed to use them properly.

Read the documentation for BufferedReader, InputStreamReader InputStream
for more information.

> Muhammad Ali Shah
> web: www.redrival.com/mufta
> email: mashah@xxxxx.xxx.xx
> [BCS V - FAST Institute of Computer Science, Karachi]