So I am supposed to complete the below program that determines the size of an array based on the int input of the user.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int userInput = -1;
int[] userData = null;
But the program starts by declaring the array as :
int[] userData = null;
It also starts by declaring the user input variable as -1:
int userInput = -1;
The problem of the program is based on re-initializing this array using the int variable scanned from the user as the length of the given array:
userInput = scan.nextInt();
So I tried to re-initizile the array using the new input:
int[] userData = new int[userInput];
But unsurprisingly Java complains since the array was initialized before (and I'm not supposed to change that).
The question is, is there actually a way to build on the given code or do I have to delete their initial declarations and start over?
userDataor use a different name, but you can't create a second variable also calleduserData.int userInput = -1;int[] userData = null;with your assignment/intialization, or don'tint[] userData = new int[userInput];but justuserData = new int[userInput];int[]in your case) in front of the variable declares a new variable with the given name. If it already exists, then of course Java will complain. However, if you leave the data type out (using onlyvariableName), then you use that variable. For example:userData = new int[userInput].