Skip to main content

while(ch == '\n' | ch == '\r');

1 reply [Last post]
mariecruie
Offline
Joined: 2011-07-16
Points: 0

Hi All,

I am trying to learn Java and am very new to programming/computing. I have a question. I would be much appriciated if you could answer it with very simple words.

do
{
System.out.println("Press a key following enter");
do
{
ch = (char)System.in.read();
}
while(ch == '\n' | ch == '\r');
}
while(ch!='q');

Qustion 1: I don't understand why the program ignores the carriage return and line feed generated by pressing enter and does not print out 2, 3 or mote times "Press a key following enter". As far as I understand the second loop does "read the character that is entered as long as if there is a line feed or carriage return". When you press enter they are both there and they are read as charachters.

do
{
System.out.println("Press a key following enter");

ch = (char)System.in.read();

}
while(ch!='q');

Question 2: When I use & insted | in "while(ch == '\n' | ch == '\r'); " the program behaves like in the second code example and prints out "Press a key following enter" as many as characters you entered + carriage return + line feed. Why?

Thank you

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ravikiran1226
Offline
Joined: 2012-09-30
Points: 0

I would recommend you to debug the program/application as you are the beginner.

New-line or carriage return characters depends upon how you machine is configured to (new line seperator).

mariecruie
Offline
Joined: 2011-07-16
Points: 0

Is there anyone who could answer my question please?

I would like to know if it is a stupid question.

Thanks