473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.print("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 28577


"Doug Lowe" <Do*******@spam-LoweWriter.com> wrote in message
news:4%iud.547511$D%.281264@attbi_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.print("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******@bellsouth.net> wrote in message
news:dv********************@bignews4.bellsouth.net ...


"Doug Lowe" <Do*******@spam-LoweWriter.com> wrote in message
news:4%iud.547511$D%.281264@attbi_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.print("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
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...
6
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...
5
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
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...
7
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...
5
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...
1
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...
6
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;...
12
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.