Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDylan Merritt
15,682 PointsI am working on a personal app of mine. I would like two arrays of Strings to match to one another.
So in my code, I have two classes, each containing an array of Strings. I use a Random selector to select an item in the array from class 1, and I would like the selection from the array in class 2 to be the same number as the Random selector selected for class 1, and the two array correspond to one another. Does anyone know how I might accomplish this or maybe a better way to do it?
2 Answers
Daniel Hartin
18,106 PointsHi Dylan
I'm just wondering why two strings that you wish to concatenate together are located in two separate classes as it does make the process a little trickier but not impossible.
I am assuming you are trying to replicate the get fact method in another class.
I would firstly try to include the second array in the method where the first array is located. You could then do your concatenation at the end of that method and return the result which would be the preferred option.
Secondly you could declare the each of the methods as public and pass the result of the random number generator into each of the method calls. Therfore using the same number each time.
I hope you understand the above, if you need help writing the code. Please paste your code back here and I can take a look through it.
Hope this helps Daniel
Ratul Sarna
Courses Plus Student 11,618 PointsHere is a possible solution -
1) Store the number you get from the random generator in a variable, say "i".
2) Create a method in your other class such as
```public String getAt (int index) {
return array[index];
}
where array is the String[] array you have in the other class.
3) Call this method with the "i" as the argument.
I hope this answers your query. Also, it would be better to provide a little more detail about your problem. Like why exactly are you having to do so with code snippets.