• Post Reply Bookmark Topic Watch Topic
  • New Topic

beginner -- InputStreamReader(System.in) question  RSS feed

 
Daniel Rodrigues
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am testing different ways to read a char from System.in to learn how it works. When I use the System.in.read() function everything goes fine but when I try to use the InputStreamReader to do the same thing the program did not complain but also does not work...

public class IOkdbTest {

static Character reply;
public static void main(String[] args) throws IOException {
char mytest;
InputStreamReader isr = new InputStreamReader(System.in);
System.out.print("Enter a character: ");
reply=Character.toUpperCase((char)System.in.read()); //THIS WORKS FINE
System.out.println("Echo: "+reply);
System.out.print("Enter another character: ");
mytest = (char) isr.read(); //THIS DOES NOT WORK
System.out.println("You typed: " + mytest);
}

}

PS: I saw that in many manuals they use this method to read from keyboard... what am I missing?

Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,

When you type in your first character, it won't show up until you press "Enter". But that Enter is an actual character, and it will still be sitting in the input buffer, waiting to be read. Your second "read()" is going to get that "Enter" character (actually a newline, "\n".) If you add an extra "System.in.read()" after the first one, but before the second one, your program will work as you expect, because that etra read() will consume the newline.
 
Daniel Rodrigues
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest

After proceeding as instructed the program worked as expected.

Thanks for your time!!!
 
Archana Honnavalli
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Hi Dan,

When you type in your first character, it won't show up until you press "Enter". But that Enter is an actual character, and it will still be sitting in the input buffer, waiting to be read. Your second "read()" is going to get that "Enter" character (actually a newline, "\n".) If you add an extra "System.in.read()" after the first one, but before the second one, your program will work as you expect, because that etra read() will consume the newline.


Thats true.
But i included Sytem.in.read(); inbetween those 2 reads
But the output is same.

Here is my code:

import java.io.IOException;
import java.io.InputStreamReader;

public class IOkdbTest {

static Character reply;
public static void main(String[] args) throws IOException {
char mytest;
InputStreamReader isr = new InputStreamReader(System.in);
System.out.print("Enter a character: ");
reply=Character.toUpperCase((char)System.in.read()); //THIS WORKS FINE
System.out.println("Echo: "+reply);
System.in.read();
System.out.print("Enter another character: ");
mytest = (char) isr.read(); //THIS DOES NOT WORK
System.out.println("You typed: " + mytest);
}

}

Please tell me where am i missing code.

Thanks
Archana
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us more than "doesn't work". What are you doing and what is happening? Are you pushing the enter key?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Archana Honnavalli wrote:

Please tell me where am i missing code.


"Real" computers, as well as environments like the Eclipse console, will just send a single "newline" character when you press Enter, as I described; but the Windows CMD.EXE console will actually send two: a carriage return followed by a newline. If you are unfortunate enough to be running the program from CMD.EXE, you'd need to put two extra reads in there.

In any case, this is a great example of why reading single characters from the command line using System.in.read() is virtually never done in real code. The Scanner class, as well as the readLine() method in BufferedReader, know how to deal with these system-dependent details, so you don't have to worry about it.
 
Archana Honnavalli
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please tell us more than "doesn't work". What are you doing and what is happening? Are you pushing the enter key?


Blue : what appears on console
Red: what i enter
In my console:
Enter a character:
a Press Enter
Echo: A
Enter another character: You typed:

I want the code to take another character from keyboard and display the same
That is possible only with the below code.

System.out.print("Enter a character: ");
reply=Character.toUpperCase((char)System.in.read());
System.out.println("Echo: "+reply);
isr.read();
System.out.print("Enter another character: ");
mytest = (char) System.in.read();
System.out.println("You typed: " + mytest);

I want to know the reason for difference in behaviour b/w 2 codes

Thanks
Archana

PS: Sorry for vague questioning. I will keep this in mind in my next post.

PS: I am using eclipse console. So "a" and "a single newline character" will be sent
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are in fact printing what you entered. Most probably a newline character (0x0a) but you might have a return character ('\r' = 0x0d) on Windows/DOS or an older Mac, as Ernest has already told you. You cannot see the newline, only that your cursor moves to the next line. Add System.out.println("***"); after that, then see where the *** appears.
 
Archana Honnavalli
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are in fact printing what you entered. Most probably a newline character (0x0a) but you might have a return character ('\r' = 0x0d) on Windows/DOS or an older Mac, as Ernest has already told you. You cannot see the newline, only that your cursor moves to the next line. Add System.out.println("***"); after that, then see where the *** appears.


Yeah i agree that.
when i type a character and press Enter
The character comes in UpperCase satisfying these lines of code.
reply=Character.toUpperCase((char)System.in.read());
System.out.println("Echo: "+reply);
And when i am pressing "Enter" in line 1
I see that its displayed after line 4 giving a new line in console.

Enter a character: a ===> 1
Echo: A ===> 2
Enter another character: *** ===> 3
You typed:
===> 4

Also suppose i give "ab" in my console and press Enter in line 1 itself
b is taken as 2nd character to read because of the below line of code and thus displayed in console
mytest = (char) isr.read();
(Provided there are only 2 read statements)

Thanks, understood the importance of newline character in this pgm.
Its surprising how simple lines like
System.out.println("***");
can help to understand so many logic.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

But have a look back at what Ernest said yesterday about which methods are usually used and which methods are not usually used. That is a useful lesson.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!