I need the user to enter a list of integers in a single line, so I wrote this:
public static Integer[] readIntegers()
{
Scanner input = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
do list.add(input.nextInt());
while(input.hasNextInt());
return list.toArray(new Integer[list.size()]);
}
but it keeps looping forever! Doesn't hasNextInt()
supposed to return false
if there are no digits on the scanner? How can I fix the previous method? Or should I use nextLine()
instead and spilt the String?