473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Console input question -- how to wait for Enter key?

In Java 1.5, what's the easiest way to get a console java app to stop until
the user presses the Enter key?

I want to do something like this:

Scanner sc = new Scanner(System. in);
String answer;
....
System.out.prin t("Press Enter to continue...");
answer = sc.next();

But unfortunately the Scanner class makes you actually enter something, it
doesn't accept an empty string.

Any help would be appreciated! TIA
Jul 17 '05 #1
2 28590


"Doug Lowe" <Do*******@sp am-LoweWriter.com> wrote in message
news:4%iud.5475 11$D%.281264@at tbi_s51...
In Java 1.5, what's the easiest way to get a console java app to stop until the user presses the Enter key?

I want to do something like this:

Scanner sc = new Scanner(System. in);
String answer;
...
System.out.prin t("Press Enter to continue...");
answer = sc.next();

But unfortunately the Scanner class makes you actually enter something, it
doesn't accept an empty string.

Any help would be appreciated! TIA

here's an example
http://leepoint.net/notes-java/41io/...10console.html

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.807 / Virus Database: 549 - Release Date: 12/7/2004
Jul 17 '05 #2
Thanks Hal. I should have been more clear in my post -- I'm pretty
experienced & know how to use BufferedReader. Its a teaching situation, so I
don't want to burden students with throws quite yet. The new Scanner class
in 5.0 is a great feature since it simplifies console apps in a standardized
way and swallows the IOException.

I was hoping maybe someone knew how to coax it into reading empty tokens, eg
the Enter key with no data input. Anyone know if its possible?

--D
"Hal Rosser" <hm******@bells outh.net> wrote in message
news:dv******** ************@bi gnews4.bellsout h.net...


"Doug Lowe" <Do*******@sp am-LoweWriter.com> wrote in message
news:4%iud.5475 11$D%.281264@at tbi_s51...
In Java 1.5, what's the easiest way to get a console java app to stop

until
the user presses the Enter key?

I want to do something like this:

Scanner sc = new Scanner(System. in);
String answer;
...
System.out.prin t("Press Enter to continue...");
answer = sc.next();

But unfortunately the Scanner class makes you actually enter something, it doesn't accept an empty string.

Any help would be appreciated! TIA

here's an example
http://leepoint.net/notes-java/41io/...10console.html

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.807 / Virus Database: 549 - Release Date: 12/7/2004

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4513
by: Paul Johnston | last post by:
I'm using VB.Net. I've tried using Console.Read() but it requires a line-terminator before it finishes (doesn't that mean it's functionally equivalent to ReadLine() -- why have it then?). I've tried Console.In.ReadBlock(), too, with no success. So, along these lines, how would I implement, simply, a "Press Any Key to Continue" routine or,...
6
2807
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform matrix by matrix (mxm) and matrix by vector (mxv) multiplication, so obviously one of my first considerations is to ask the user if they want an mxm or...
5
6624
by: WildHare | last post by:
I have a console application that I want to continue to run until I press a key. How can I get a keypress without stopping execution? Thanks.
3
8965
by: Wayne | last post by:
I have a console application (ssh2.exe) that requires keystrokes to be sent to it after activating. I have worked that it needs to be started in it own thread, but capturing the process and sending the keystrokes escapes me at the moment. Is there any material online that describes the process of sending keystrokes to a console app?
7
1719
by: John Salerno | last post by:
Ok, here's an attempt at something. I figure I can use this to let me know when my laundry's done! :) I'm hoping you guys can spot ways to make it better/cleaner/more efficient, etc. especially where the math is involved. Thanks. using System; using System.Threading;
5
6327
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a complete project to show what happens. If anybody has an idea of how to fix this, please let me know. Yes I am aware that this is all in .Net 2....
1
1671
by: Teis Draiby | last post by:
I am working on a Win32 program with an additional console window to output messages. I also want to type commands in the console window. Is there any common way to achieve that? For example how do I output messages while the console is listning for command inputs? I'm looking forward for your answer with great anticipation
6
4699
by: JM | last post by:
Hi, I need a function for a character based program (console) that works exactly like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then move on. Console.Read; Console.ReadLine;.... all read input until the user hits <Enter>. For example, if you get the question "Do you wish to continue? (Y/N)", from the moment the user...
12
3734
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would also allow my application to display a Console, but i don't seem to be allowed to do this. I don't see any References that i need to add, so how...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.