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

Discussion in 'Java' started by Shanknbake, Oct 7, 2005.

  1. Shanknbake

    Shanknbake Guest

    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
     
    Shanknbake, Oct 7, 2005
    #1
    1. Advertisements

  2. Shanknbake

    Roedy Green Guest

    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.
     
    Roedy Green, Oct 7, 2005
    #2
    1. Advertisements

  3. Shanknbake

    Shanknbake Guest

    okay thanks...that helps a lot!! my program works flawlessly now :)
     
    Shanknbake, Oct 8, 2005
    #3
  4. Shanknbake

    Roedy Green Guest

    Good. Iove it when a student can run with the ball given just a small
    clue like that.
     
    Roedy Green, Oct 9, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.