![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 10
Rep Power: 0
![]() |
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;
import java.util.Scanner;
public class Simplify {
public Object [] UseArrayList(){
ArrayList <Double> collectInputs = new ArrayList<Double>();
Scanner variNum = new Scanner(System.in);
Double makeNum = null;
String endingCommand = "";
boolean ctrlD = true;
try{
while(ctrlD){
endingCommand = variNum.next();
makeNum = Double.parseDouble(endingCommand);
collectInputs.add(makeNum);
}
}
catch(Exception e){
System.err.println("\nFailed to read input: "+endingCommand);
if(collectInputs.isEmpty()){
System.err.println("\nNo elements in the set.");
System.exit(1);
}else{
return collectInputs.toArray();
}
}
return null;
}
} |
|
|
|
|
|
#2 |
|
Programmer
|
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.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2008
Posts: 10
Rep Power: 0
![]() |
Re: Using ctrl+d to end input
How do you detect end of file (EOF)?
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2007
Posts: 73
Rep Power: 1
![]() |
Re: Using ctrl+d to end input
maybe change while (ctrlD) to while (variNum.hasNext())
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2008
Posts: 10
Rep Power: 0
![]() |
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.
/*
* import statements go at the top
*/
//import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/*
* javadoc goes here for author and description of the class and...
*/
/**
* enter description of the class here:
* @author Anonymous
*/
/*
* This is how you make a public class
*/
public class Simplify {
/*
* javadoc goes here for description of the method and/or
* parameters (if any) and...
*/
/**
* enter description of the method here:
*/
/* this is how you make a public method named UseArrayList that
* returns an array made up of objects
*/
public Object[] UseArrayList(){
/*
* An arrayList is a list you can use like an array... it
* grows as you need, because it is technically a list
*/
ArrayList <Double> collectInputs = new ArrayList<Double>();
Scanner variNum;
boolean ctrlD = true;
String instruct = "\nPlease enter a series of numbers, " +
"each followed \nby the return key. When you are finished,"+
" use the\nkeyboard command ctrl+d to stop inputing " +
"numbers";
/*
* instructions to the user
*/
System.out.println(instruct);
/*
* a try is to identify an error before it crashes your
* program. Because I am parsing an input to a double,
* I need to make sure it catches non-number entries
* without crashing the program.
*/
try{
// variNum = new Scanner(new FileReader("Information"));
variNum = new Scanner(System.in);
/*
* this 'while' is always true. This means it will loop
* forever unless I either use ctrl+d or give it something
* the try will choke on, like a non-number for instance.
* notice I made a variable named ctrlD and used it instead
* of just writing true. This is on purpose to allow me to
* set the variable inside instantiated objects to false if
* I wanted to.
*/
while(ctrlD){
/*
* Parse a string input to a double and add it to the
* arrayList in one shot
*/
collectInputs.add(variNum.nextDouble());
/*
* closes my while loop
*/
}
/*
* closes my try statement
*/
}
/*
* every 'try' must have a 'catch'.
*/
catch(Exception e){
/*
* check for empty array because you can't use toArray() on
* an empty arrayList
*/
if(collectInputs.isEmpty()){
/*
* Just writing something so if it happens I see it.
*/
System.err.println("\nNo elements in the set.");
/*
* abandon the program if nothing was entered. The
* integer 1 is to signal a program failure.
*/
System.exit(1);
/*
* closes my if statement
*/
}else{
/*
* make a real array (i.e. not arraylist)
*/
return collectInputs.toArray();
/*
* closes my else statement
*/
}
}
/* because I have to. The method needs a return no matter
* what and the 'else' does not have one so if the program
* were to go to the else statement the compiler thinks the
* code would lead to a non-return, thus I write a bogus
* return here to satisfy the compiler.
*/
return null;
/*
* closes my method
*/
}
/*
* closes my class
*/
}Last edited by dimlyBright; Apr 5th, 2008 at 4:41 PM. |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 165
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2008
Posts: 10
Rep Power: 0
![]() |
Re: Using ctrl+d to end input
Thanks for all the suggestions. I think I have it working up to par now.
Cheers, dB |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Input Output Function Error | kewlgeye | Visual Basic | 9 | Jan 26th, 2008 3:31 PM |
| Clearing the input box | 357mag | Java | 5 | Nov 1st, 2007 9:28 PM |
| can someone please help me with this tree | cwl157 | Java | 19 | Oct 27th, 2007 12:35 PM |
| Reading character input into an array (raw mode) | shoeyfighter | C | 3 | Nov 2nd, 2006 3:49 PM |