I'm trying to read in a list of Integers from a scanner, using user input, and store all input into an ArrayList, except the last integer the user inputs, of which I want to store within an Integer, (Integer key;) How can I do this?
I've tried putting it into a int[] first but I don't know how many integers the user will input. Is there a way to directly read in all integers to the ArrayList excluding the last one, which would be stored within the Integer key?
What I have so far:
private ArrayList<Integer> theList;
private Integer theKey;
public static void main(String args[])
{
int[] integers = null;
System.out.println("Enter a list of integers, the final being a key");
Scanner scanner = new Scanner(System.in);
int i = 0;
try
{
while (scanner.hasNext())
{
integers[i] = scanner.nextInt();
i++;
}
}
catch (NullPointerException ex)
{
}
for (int k = 0; i < integers.length; i++)
{
System.out.print(integers[k] + " ");
}
}