Hey everyone, I have a quick question about some Java code.
I need to write a method that fills an array, with a few conditions.

The method I'm writing is called readArray.
There are two arrays, one called "states" and the other "capitals". I have to fill both these String arrays using ONE method. I can handle that, except I have to fill them using information from a file. The first line in the file is a state, the second is it's the capital...and so on. Do I create the arrays in the main method and pass them to readArray as parameters? Or do I create the arrays inside of the readArray?

I am completely lost with this. Can someone help? Thanks :)

Edited 5 Years Ago by loupgarou: n/a

best would be to create the arrays on class level, not within a method. that way, you don't have to pass them as parameters, and still use them within several methods.

do you know up front how many elements the files have? if so, instantiate the arrays when you declare them, otherwise, instantiate them within the method filling the values.

In java you can only return one thing from a method. You can't return two arrays (unless you package both those arrays inside a single object and return that, which is probably a bit excessive for this case).
However, when someone passes an array into a method as a parameter, you can access, use, or update the elements of that array. And because you can have any number of parameters, you can update any number of arrays like that.
So create two arrays in main, pass them as parameters to your readArray method, and store all your states and capitals in the arrays you were passed.
Since main doesn't know how big the file is, you'll just have to create two arrays that are "big enough" and maybe have readArray return a single int that says how many states/capitals have been stored in them.

ps: Stultuske's post came in while I was typing. Between us we have given you two approaches, both equally valid. It probably doesn't matter which you chose, but you may find his easier as a beginner. In any case you should understand the stuff I was explaining about parameters and return values.

Edited 5 Years Ago by JamesCherrill: ps

In java you can only return one thing from a method. You can't return two arrays (unless you package both those arrays inside a single object and return that, which is probably a bit excessive for this case).
However, when someone passes an array into a method as a parameter, you can access, use, or update the elements of that array. And because you can have any number of parameters, you can update any number of arrays like that.
So create two arrays in main, pass them as parameters to your readArray method, and store all your states and capitals in the arrays you were passed.
Since main doesn't know how big the file is, you'll just have to create two arrays that are "big enough" and maybe have readArray return a single int that says how many states/capitals have been stored in them.

ps: Stultuske's post came in while I was typing. Between us we have given you two approaches, both equally valid. It probably doesn't matter which you chose, but you may find his easier as a beginner. In any case you should understand the stuff I was explaining about parameters and return values.

Actually that makes complete sense. Thanks!
One other quick question. Is there a way to fill an array with every other line from the file?

This question has already been answered. Start a new discussion instead.