![]() |
Using ctrl+d to end input
What is the simplest way to implement a program that uses a loop to ask for a variable amount of inputs and when you are through giving it inputs you use the keyboard command "ctrl+d" ("ctrl+z" for windows).
Here is what I have and it crashes (throws and exception) every time i use "ctrl+d". What it does exactly is throw an exception, erase the contents of the array list and fall into the if statement and exits because the array is empty... I can't explain why it is doing all that. I just wanted to end the input loop with ctrl+d... I know how to end it by other means (i.e. a quit command like "qq"). :
import java.util.ArrayList; |
Re: Using ctrl+d to end input
I don't know much about Java, but I believe Ctrl-D is not a signal in Unix, but rather the EOF. Instead of trying to detect the keypress, you could try and detect the EOF.
|
Re: Using ctrl+d to end input
How do you detect end of file (EOF)?
|
Re: Using ctrl+d to end input
maybe change while (ctrlD) to while (variNum.hasNext())
|
Re: Using ctrl+d to end input
Using Scanner's nextDouble() and hasNextDouble() methods will obviate the need to manually parse each double.
An EOFException is thrown when the end of a file is encountered. |
Re: Using ctrl+d to end input
This seems to work... I still don't think it is written correctly. I could not implement hasNextDouble() for some reason. Every time I hit ctrl-z (ctrl-d for linux) it crashed. I wrote comments to help me read it and know what is happening.
:
/* |
Re: Using ctrl+d to end input
Like was already said, the hasNext type methods will check to see if there is input that matches and once there isn't it will terminate the loop. for example
while ( keyboard.hasNextInt() ) { keyboard.nextInt(); System.out.println("read in an integer"); } if you enter something like a-z, it will terminate. or you can press ctrl+z for windows and it will terminate. If you're looking for the simplest solution that answers your question. If you're stuck on figuring out why ctrl+d isn't working I'm not sure. |
Re: Using ctrl+d to end input
Thanks for all the suggestions. I think I have it working up to par now.
Cheers, dB |
| All times are GMT -5. The time now is 1:05 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC