1

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?

4
  • 2
    You can't declare the same variable twice. You can assign a new value to userData or use a different name, but you can't create a second variable also called userData. Commented Nov 15, 2021 at 20:59
  • hi & welcome! :) Or replace int userInput = -1;int[] userData = null; with your assignment/intialization, or don't int[] userData = new int[userInput]; but just userData = new int[userInput]; Commented Nov 15, 2021 at 21:00
  • 1
    Putting the data type (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 only variableName), then you use that variable. For example: userData = new int[userInput]. Commented Nov 15, 2021 at 21:01
  • @MCEmperor Oh!! It now worked! I wrote userData without the braces and the program is now fine. Thanks a lot. And thank you all. First post always sounds dumb :D. Commented Nov 15, 2021 at 21:27

2 Answers 2

1

It may be better to declare and initialize the variables as soon as you need them with appropriate values without having to reassign them:

Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt(); // no need to set to -1
int[] userData = new int[userInput]; // no need to set to null
Sign up to request clarification or add additional context in comments.

1 Comment

I guess working with already declared variables was part of the exercise - regardless of what is the best practice.
1

You can continue writing your program like so:

int[] userdata = null;
userInput = … ;
userdata = new int[userInput];  // Create new array, and assign to the existing variable. 

Verify.

System.out.println(userdata);
System.out.println(userdata.length);

The output of this code is similar to:

[I@3d3fcdb0
10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.