Skip to content
Advertisement

Java using scanner for user input to set size of array

I want to allow for the user to be able to choose the size of the int array in my program

Right now I have the following code:

JavaScript

I’m not sure why but when I go to test and print my Array.toString it simply prints ”'[]”’ (obviously it is not doing what it should be doing and populating the int array).

Does anyone have any suggestions on how I can tidy my code smell up and make it so this actually works?

Advertisement

Answer

You have created the array already, and your setArraySize changes the size variable only.

JavaScript

Change to something similar to this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement