You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:
public void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while(true) { String word = console.readLine(); if (word.equalsIgnoreCase("end") { break; } else { numbers.add(Integer.parseInt(word); } }
Ofcourse you won’t use while(true)
and you won’t put this in main
, but it’s just for the sake of the example