   [java] Wait for User to Press Enter
  I know that this seems really stupid, but what code is necessary for a Java program to wait until the user presses enter after some text is displayed? There just does not seem to be any working resources on this at all. Thanks in advance for any help.   
  The class must implement KeyListener and one of the first few lines of code must be "addKeyListener(this);" (Without the quotes). You must include the void methods  keyPressed(KeyEvent e), keyTyped(KeyEvent e), and keyReleased(KeyEvent e).In the keyPressed (or keyReleased, your preference) method you must have the codeif(e.getKeyCode()==10)//10 is the keyCode for enter.closeText();//Put what you want to do when they hit enter here.   ;  Just a tip:Never use literals to represent key values (or any event, for that matter). These could change in a future version of Java.  The preferred approach would be KeyEvent.VK_ENTER .   ;  OK, I've tried using all of the stuff you gave me and implement it into a simple Hello World demo first, but it won't compile because of an "illegal start of expression" at line 6. Here is my code:public class KeyStrokeTest implements KeyListener{public static void main (String[] args) {//KeyStroke codeaddKeyListener(this); void keyPressed(KeyEvent e) {if (e.getKeyCode()==KeyEvent.VK_ENTER) {closeText();}}void keyTyped (KeyEvent e) {}void keyReleased (KeyEvent e) {} System.out.println ("Hello World!");}}   ;  You are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.public class KeyStrokeTest implements KeyListener{    void keyPressed(KeyEvent e)    {   if (e.getKeyCode()==KeyEvent.VK_ENTER)   {  closeText();   }    }    void keyTyped (KeyEvent e)    {    }    void keyReleased (KeyEvent e)    {    }    public static void main (String[] args)    {   //KeyStroke code   addKeyListener(this);   System.out.println ("Hello World!");    }};Quote:Original post by Simian ManYou are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.public class KeyStrokeTest implements KeyListener{    void keyPressed(KeyEvent e)    {   if (e.getKeyCode()==KeyEvent.VK_ENTER)   {  closeText();   }    }    void keyTyped (KeyEvent e)    {    }    void keyReleased (KeyEvent e)    {    }    public static void main (String[] args)    {   //KeyStroke code   addKeyListener(this);   System.out.println ("Hello World!");    }}Thanks for the help, but will I have to do anything in particular if it's in an applet as that's what it's eventually going to be placed in? Also the code you game me does not compile either.   ;  Hi, Is it a console app?Because this will work just fine.  public static void main(String[] args) {    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));    System.out.println("enter somethin:\nasd");    try { String str = r.readLine(); //will wait for a new line;    } catch (IOException e) { e.printStackTrace();    }  }The function readline of BufferedReader wait for a new line to continue. In our case enter would be a newline. I have not tested that on unix but I'm pretty sure it can work!   ;  GTspeed is correct in his code (just missing "the rest of the story" like components and windows and such.  Here is a better tutorial that should help:http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.htmlGood luck.   ;  Well if you want to use it in an applet here is a quick helloworld applet with the additional functionality you are looking for.import java.awt.*;import java.awt.event.*;import javax.swing.JApplet;public class HelloWorldApplet extends JApplet implements KeyListener {private boolean displayText;public HelloWorldApplet() {this.addKeyListener(this);}public void init() {displayText = true;}public void paint( Graphics g ) {if( displayText )g.drawString("Hello World!",10,10);elseg.clearRect(0,0,this.getWidth(),this.getHeight());}public void keyPressed( KeyEvent e ) {}public void keyReleased( KeyEvent e ) {if( e.getKeyCode() == KeyEvent.VK_ENTER ) {displayText = !displayText;this.paint(this.getGraphics());}}public void keyTyped( KeyEvent e ) {//mainly used for standard editing characters}}Now the source for the page that will display the applet<html><head><title>Hello World Applet Test Page</title></head><body><appletcode="HelloWorldApplet"width="400"height="400">If you are seeing this text you need to download<a href="http://java.sun.com/">Java</a>in order to properly view this web page. Please, follow the link provided!</applet></body></html>I compiled and tested it and it works! Hope that helps!   ;Quote:Original post by Simian ManYou are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.This is true, but where did this "nested function" concept come from?  Does C++ support nested functions?  If so, how are they used?   