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;
}
}