How Do I Detect if the User has pressed the ENTER key???

S

Shanknbake

Hi, this is my first java class..and I'm writing a program in which i
need to allow the user to "continue by pressing the enter key, or q to
quit"...how to I check if the user has entered the enter key?

Code:

do
{
//...body code
//...body code
//bottom of do/while statement

System.out.println("If you would like to quit, type 'q', otherwise
press any [enter] to continue");
quit=keyboard.next();


}
while(quit.equalsIgnoreCase("\n")); //how do i check if enter key
is pressed?
}
}...

quit is a String variable...not a char
plz help this
thanks
 
R

Roedy Green

Hi, this is my first java class..and I'm writing a program in which i
need to allow the user to "continue by pressing the enter key, or q to
quit"...how to I check if the user has entered the enter key?

Java console does not support key by key reading. It supports line by
line reading.

See http://mindprod.com/applets/fileio.html
for details

And empty line is "" length 0 which is just an entry without other
data.
 
R

Roedy Green

okay thanks...that helps a lot!! my program works flawlessly now :)

Good. Iove it when a student can run with the ball given just a small
clue like that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top