The Artima Developer Community
Sponsored Link

Java Answers Forum
ending a loop with input?

2 replies on 1 page. Most recent reply: May 9, 2006 4:15 PM by Mike Trouble

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Mike Trouble

Posts: 3
Nickname: jeps
Registered: May, 2006

ending a loop with input? Posted: May 8, 2006 5:40 PM
Reply to this message Reply
Advertisement
How can I create a do-while loop, that stops running when I press enter?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: ending a loop with input? Posted: May 8, 2006 10:45 PM
Reply to this message Reply
You need a condition
boolean enterPressed = false;
at the beginning of the loop.
You now have two possibilities, but I'm afraid I cannot tell you how to implement them (have to read the API myself). Just to give you an idea.

1. There is a key buffer for all typed keys. At the end of the loop check if this buffer is empty. Otherwise read it and check if someone pressed Enter.

2. Swing supports a KeyEvent which is launched at every key pressed. Maybe you can use the same mechanics for your text interface.

In both cases just set enterPressed = true, if the Enter key was typed.

Mike Trouble

Posts: 3
Nickname: jeps
Registered: May, 2006

Re: ending a loop with input? Posted: May 9, 2006 4:15 PM
Reply to this message Reply
Thx Neumair, got 1. to work nicely.

Flat View: This topic has 2 replies on 1 page
Topic: system properties vs environment variables Previous Topic   Next Topic Topic: CAISI Project: Can developers end homelessness?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use