Computer Science, asked by mayankghatpande8683, 1 year ago

How to define an array size in java without hardcoding?

Answers

Answered by Saimrock78
1

I'm having a problem where I cannot initialize an array without hard-coding it in Java.

So this is the hard-code version which works:

int numberOfElements = inputFromFile(args[0], myArray);

int [] myArray = new int[1000];

inputFromFile is basically going to be a method I'm going to write that can read the numberOfElements from a textfile along with my program.

I tried fixing it without hardcoding and got my result to be this:

int numberOfElements = inputFromFile(args[0], myArray);

int [] myArray = new int[numberOfElements];

Problem remains that Eclipse wants me to initialize myArray which is an integer array. Is there any good fixes without hard-coding? Any suggestion would be great.

Similar questions