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 trial
Matheus G Oliveira
9,682 PointsHelp to generate Random Strings but not equal
Hi guys,
I have this class, RandomSpace1 and when i click the button i would like to get not equal random Strings with the value to be defined by 1, 2, 3, 4 or 5.
Lets resume, if i click the button Two, it will bring me Two random Strings that are not equal on the textviews 1 and 2. Ex.: "Earth", "Saturn"
If i click Four, it will bring me 4 random Strings that are not equal on the textview 1, 2, 3 and 4 . Ex.: "Mars", "Earth", "Saturn", "Uranus".
public class RandomSpace1 {
// member variables (properties about the object)
public String[] answerOne = {
"Venus",
"Mars",
"Jupiter",
"Earth",
"Saturn",
"Uranus",
};
public String getAnAnswer(){
String answerOne = "";
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(teamsOne.length);
answerOne = teamsOne[randomNumber];
return answerOne;
}
}
anyone can help?
7 Answers
Jesse Liptak
1,118 PointsgetAnswer() returns an array of Strings. You would also have to give getAnswers() an int as a parameter.
Try:
String answerOne[] = mRandomSpace1.getAnswers(<insert number here>);
If that doesn't work and you are only using RandomSpace1 for the random names, you could also try adding static in front of the two methods and one class variable in RandomSpace1 as follows:
public static String[] answerOne = { // Added keyword static
"Venus",
.....
public static String[] getAnswers(int numberOfAnswers){ // And here
String answers[] = new String[numberOfAnswers];
int randomNumbers[] = new int[numberOfAnswers];
.....
private static boolean unique (int[] arrayIn) { // and here
for (int i = 0; i < arrayIn.length - 1; i++) {
for (int j = i + 1; j < arrayIn.length; j++) {
.....
Then you can call the method as:
String answerOne[] = RandomSpace1.getAnswers(<insert number here>);
Jesse Liptak
1,118 PointsI have modified your code to make it work as shown below. This will return x amount of unique answers based on what number you give to getAnswers() as a parameter.
Note: You can only pass in a number between 0 and answerOne.length or it will crash.
import java.util.Random;
public class RandomSpace1 {
public String[] answerOne = {
"Venus",
"Mars",
"Jupiter",
"Earth",
"Saturn",
"Uranus",
};
////////////////////////////////////////////////////////
// Method to get random answers from answerOne[]
// Parameters: number of answers to return
// Returns: array of Strings containing answers
////////////////////////////////////////////////////////
public String[] getAnswers(int numberOfAnswers){
String answers[] = new String[numberOfAnswers];
int randomNumbers[] = new int[numberOfAnswers];
Random randomGenerator = new Random();
if (numberOfAnswers > 1) {
do {
for (int i = 0; i < numberOfAnswers; i++) {
randomNumbers[i] = randomGenerator.nextInt(answerOne.length);
}
} while (!unique(randomNumbers));
} else {
randomNumbers[0] = randomGenerator.nextInt(answerOne.length);
}
for (int i = 0; i < numberOfAnswers; i++) {
answers[i] = answerOne[randomNumbers[i]];
}
return answers;
}
////////////////////////////////////////////////////////
// Method to check whether or not all values are unique.
// Parameter: array of integers
// Returns: boolean
////////////////////////////////////////////////////////
private boolean unique (int[] arrayIn) {
for (int i = 0; i < arrayIn.length - 1; i++) {
for (int j = i + 1; j < arrayIn.length; j++) {
if (arrayIn[i] == arrayIn[j]) {
return false;
}
}
}
return true;
}
}
If there is someone that has a better solution, please feel free to let me know. I am no java expert! I hope this answers your question accurately Matheus!
Jesse Liptak
1,118 PointssetText() takes in a string, not an array of strings. Therefore if you want to put all the returned answers on one TextView, you would have to do something along the lines of this:
String answerOneStr = "";
for (int i = 0; i < answerOne.length; i++) {
answerOneStr += answerOne[i] + ", ";
}
This will make a String containing planet1, planet2.... From there you can use that string in your setText method.
Matheus G Oliveira
9,682 PointsSorry man, but where should i put this?
Jesse Liptak
1,118 PointsInstead of this:
private void handleNewAnswer2() {
String answerOne[] = mRandomSpace1.getAnswers();
mplrOne.setText(answerOne);
use:
private void handleNewAnswer2() {
String answerOneStr = "";
for (int i = 0; i < answerOne.length; i++) {
answerOneStr += answerOne[i] + ", ";
}
mplrOne.setText(answerOneStr);
Sorry for posting so many "answers" by the way. I just realized there was a comment button that I should have been using. Hope this helps.
Matheus G Oliveira
9,682 PointsThanks very much for your answer. Stil im having problems
im calling this this method getAnswers from another activity which i put this
private void handleNewAnswer2() {
String answerOne = mRandomSpace1.getAnswers();
mplrOne.setText(answerOne);
}
and it tells me: The method getAnswers(int) in the type RandomSpace1 is not applicable for the arguments () ActivityAnswer.java
after that my eclipse did crash. would it be possible for you to explain me the code or why is it saying that it cant find the getAnswers happening? i think im missing something about where to put it right.
Matheus G Oliveira
9,682 PointsGot it, still return an error on the activity which i call the method
private void handleNewAnswer2() {
String answerOne[] = mRandomSpace1.getAnswers();
mplrOne.setText(answerOne);
}
it says the method setText(CharSequence) in the type TextView is not applicable for the arguments (String[])
that is just to show the result... i created another activity to get the answers on textviews on the screen, then set
TextView mplrOne;
and
mplrOne = (TextView) findViewById(R.id.plrOne);
on the onCreate method, together with the
handleNewAnswer2();
Matheus G Oliveira
9,682 PointsHey Jesse,
Do you know if its posible to use this code you showed me (below) to have the answer in more than one textview? For example, if i have 3 random strings. each random strings will go to one different textview... if i have 4 random strings then it will show 4 random strings on 4 different textviews.
the one below is making all the answers go to only one textview.
private void handleNewAnswer2() {
String answerOneStr = "";
for (int i = 0; i < answerOne.length; i++) {
answerOneStr += answerOne[i] + ", ";
}
mplrOne.setText(answerOneStr);
Jesse Liptak
1,118 PointsThe array answerOne contains the number of answers you wanted returned. So for an example, if you wanted four answers and the method returned an array containing "Earth, Neptune, Mars, Uranus", you can access them but using the array variable name method followed by their index in square brackets. I would access Neptune by using the following:
System.out.println(answerOne[1]); // This would print "Neptune"
So instead of using the for loop, just assign the answer array values to previously created TextViews as follows:
private void handleNewAnswer2() {
mplrOne1.setText(answerOne[0]);
mplrOne2.setText(answerOne[1]);
mplrOne3.setText(answerOne[2]);
mplrOne4.setText(answerOne[3]);
Remeber though that if you try to access a value outside the array's size that you will get an array out of bounds exception. Example, you ask for 3 random answers but you still try and assign them to the four TextViews. That will mean that this line
mplrOne4.setText(answerOne[3]);
Will throw an error because you are trying to assign a piece of the array that doesn't exist.
Hope this helps!
Matheus G Oliveira
9,682 PointsJesse, you are God!
Thank you again!
Matheus G Oliveira
9,682 PointsMatheus G Oliveira
9,682 PointsAppreciated man!!!! Thank you very much for the help!!! Thanks for sharing your knowledge ;)