Reputation: 143
I'm trying to write a program which is a game that generates random letters and the user selects whether random generated letters are part of user's name. I have not started the second algorithm, I want to focus on the first one.
UPDATE: Hey, so I followed you guys advice and I was able to get the algorithm 1 to work. Now the only thing for me to finish algorithm 1 is to have a loop that checks whether the randomly generated letter is in the wrongLetters arrayList. If it is, then it would generate another random letter and if its not part of the arrayList, it would ask the user whether its the next letter. The wrongLetters arraylist should hold several strings (letters) that are not the next letter in user name. As soon as a letter is correctly guessed, it would clear the wrong letters arraylist and add new strings when guessing for the next letter after that.
I tried using a do/while loop using while(actualLetter != wrongLetters);
but I keep getting an error about "incompatible operand types". How can i fix this? thanks again.
Thanks again!
/*
* PSEUDOCODE:
* -Ask user to choose an option (algorithm 1, algorithm 2, quit)
* -if user selects algorithm 1, generate a random number to pass off as unicode(char) and then change char to string
* -ask user whether generated char is part of their name
* -if yes, then add (actualLetter) string to arrayList(name).
* -if no, then add (actualLetter) string to arrayList(wrongLetters).
* reask user to choose an option (algorithm 1, algorithm 2, quit)
* whether user chooses algorithm 1 or 2, after generating random char, check char to make sure its not in the arrayList(wrongLetters)
* if char is in the wrongLetters arrayList, then generate another random char until a char that is not in that array generates
*
*
*/
import javax.swing.JOptionPane;
import java.util.ArrayList;
public class Lab_1 {
public static void main(String[] args) {
int n;
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> wrongLetters = new ArrayList<String>();
String result = "";
int p = 1;
Object[] options2 = {"No", "Yes"};
do{
do{
Object[] options = {"Quit", "Algorithm 2", "Algorithm 1"};
n = JOptionPane.showOptionDialog(null, "Please choose an option.", "A Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (n != 0){
if (n == 1){
JOptionPane.showMessageDialog(null, "You have chosen algorithm 2.");
}
else if (n == 2){
algorithm1(name, wrongLetters);
p = JOptionPane.showOptionDialog(null, "Are there any letters remaining in your name?", "A Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options2, options2[0]);
//if the answer is no
if(p == 0){
for(String str: name){
result=result + str;
}
JOptionPane.showMessageDialog(null, "Your name is " + result);
}
}
}
else {
JOptionPane.showMessageDialog(null, "Thanks for playing, " + result + "!");
}
}while(p == 1);
}while (n != 0);
}
public static void algorithm1(ArrayList<String> name, ArrayList<String> wrongLetters){
String actualLetter = "";
do{
int unicode = (int) (Math.random() * 25) + 65; //comes up with random number to use as a char
char aLetter = (char) unicode;
actualLetter = Character.toString(aLetter);
Object[] options = {"No", "Yes"};
int o;
o = JOptionPane.showOptionDialog(null, "Is this the next letter in your name: " + aLetter, "A Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
//if you choose yes
if(o == 1){
name.add(actualLetter);
}
//if you choose no
else if (o == 0){
wrongLetters.add(actualLetter);
}
}while(actualLetter != wrongLetters);
}
}
Upvotes: 0
Views: 276
Reputation: 549
JOptionPane.showMessageDialog(null, name);
this sentence if wrong, it cann't show the names in name,because name is a variable of list,not a String. you can do like this :
String result="";
for(String str: name){
result=result+";"+str;
}
JOptionPane.showMessageDialog(null, result);
Upvotes: 1
Reputation: 1615
You're storing the letters in ArrayLists that are local variables in algorithm1
. Once you've reached the end of that function you lose both lists along with whatever values you previously had stored in them.
I don't know if this is an elegant solution but you could declare both ArrayLists in your main method and then pass them to your algorithm methods.
You'd need to change the method signature for algorithm1
like so:
public static void algorithm1(ArrayList<String> name, ArrayList<String> wrongLetters)
And then change your call to algorithm1 to this, assuming that you named your ArrayLists name
and wrongLetters
respectively:
algorithm1(name, wrongLetters);
Upvotes: 0