I wanted to extract input from my scanner and insert it directly into a typesafe arraylist. Here is my code:
public static void main(String args[])
{
List<Integer> arglist=new ArrayList<Integer>();
List<Integer> resultlist=new ArrayList<Integer>();
Scanner in=new Scanner(System.in);
System.out.println("Scanning");
while(in.hasNext())
{
arglist.add(in.nextInt());
}
System.out.println("Scan complete, sorting...");
System.out.println(arglist);
resultlist=qksort(arglist);
System.out.println(resultlist);
}
This works only when I explicitly terminate input using Ctrl+Z. Shouldn't the stream terminate when I press "Enter"?
System.in. It will work with a file as input, but withSystem.inyou will only have the EOF flag set when you hit Ctrl + Z. Yourwhilebody will run any time you press Enter, for every word in the line, but thehasNextmethod will wait for the OS return again, so, unless you terminate the console input, it will wait forever.