0

So I'm creating a riddle app that when you swipe up on the screen it will come up with a riddle that is randomly generated using an array. Now I have no idea which riddle will pop up because of course, it's random but I want the user to be able to click and hold on the randomly generated riddle and the answer will then pop up replacing the riddle. So far I have the array done to where it will randomly generate a riddle but I'm having a problem getting the answer to match up with riddles. How would I go about matching correct answers to a random array on a long press of the text view?

I did see two other questions (one,two) and similar to this one but it did not answer the question that I have and it really wasn't specific.

final TextView answerTxt = (TextView) findViewById(R.id.answerTxt);


    //List with the riddles and its values
    HashMap<String, String> h = new Hashmap<String, String>();
    h.put("a", "v");
    h.put("b", "w");
    h.put("c", "x");
    h.put("d", "y");
    h.put("e", "z");

    //rand generator for array
    String[] yourKeyList = h.keySet().toArray(new String[]{});

    String[] yourValueList = h.values().toArray(new String[]{});

    String randomKey = yourKeyList[new Random().nextInt(yourKeyList.length)];

    final String keyValue = yourValueList[yourValueList.length];



    answerTxt.setText(randomKey);

    answerTxt.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            answerTxt.setText(keyValue);
            return true;
        }
    });
Mac
  • 15
  • 1
  • 8

2 Answers2

0

The array of riddles and the array of answers should be linked. When the riddle pops up you should know what index was chosen, and then use that as the index into the answer.

Kevin
  • 319
  • 2
  • 10
0

I suggest you use hashmap, rather than arraylist of strings. The basic format of a hashmap is as follows:

HashMap h <String, String> = new Hashmap<String, String>();
h.put("riddle", "answer");
h.put("riddle", "answer");
h.put("riddle", "answer");
h.put("riddle", "answer");
h.put("riddle", "answer");

To get the answer from the randomly generated hashmap, you would simply do:

String answer = h.get("riddle")

There you have it. To get the random hashmap maybe do something like this:

String randomValue = (String) values[(new Random).nextInt(value.length)]; 

This gets you a random value within the range of your hashmap:

String randomRiddle=h[randomValue]

Do this, get back to me, tell me how it went.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • I definitely see how a hashmap will be the answer to my problem but how would I call just the key (at random) instead of the actual value for that key. Then when the user taps the key it will then present the value for that key. I tried searching for ways to do it because I actually didn't know what a hashmap was before you said it but the ways that I found is pretty much listing all the keys, other ways I've seen I'm not familiar with. I'm fairly new to Android development but like I said I definitely feel like thats what I need to make it work. – Mac Jul 10 '17 at 04:02
  • @Mac This can be a pretty cool learning experience for you then, especially if you've never used Maps before—they're very useful! In order to get JUST the key at random, do the following `String[] yourKeyList = h.keySet().toArray(); String randomKey = yourKeyList[new Random().nextInt(yourKeyList.length)];;` This piece of code above basically takes a random number as an index to produce a random key. We are taking all the keys from hashmap, putting it in an array list, and then grabbing a random one. Respond to this comment to let me know if it worked! – Ruchir Baronia Jul 10 '17 at 19:26
  • So just tried the code above and it works perfectly. Now I just need help with showing the value of the key when the user clicks and hold on it. So far this is the code (updated in the question) that I have to try and make it work but unfortunately, the app keeps on crashing when I get to the activity. – Mac Jul 11 '17 at 06:10
  • @Mac Yay, it worked! Please accept the answer (green check mark) so others having the same issue can solve it using my answer – Ruchir Baronia Jul 11 '17 at 07:17
  • @Mac Regarding your second issue: What is the error you are getting when it crashes? Paste the logcat. – Ruchir Baronia Jul 11 '17 at 07:18
  • Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5 at com.kadamac.aora.Main2Activity.onCreate(Main2Activity.java:114) – Mac Jul 11 '17 at 14:02
  • It's showing that something is wrong with this line of code....**final String keyValue = yourValueList[yourValueList.length];** – Mac Jul 11 '17 at 14:05
  • try doing `yourValueList.length-1` instead of `yourValueList.length` – Ruchir Baronia Jul 11 '17 at 19:39
  • so that did stop the app from crashing but now every time click and hold on the key (no matter what key it is) it just shows the value for the last listed key. – Mac Jul 12 '17 at 02:12
  • @Mac I need to look at all your code to help you, post it as an edit in your question – Ruchir Baronia Jul 12 '17 at 17:30
  • @Mac Sorry, didn't see that! Try storing that random value generated as an integer, and then referencing that index for both your arrays: `int myRandomIndex = new Random().nextInt(yourKeyList.length)];`. Now, you can get the key like this: `String key = keyList[myRandomIndex]` and `String value = valueList[myRandomIndex]` – Ruchir Baronia Jul 12 '17 at 19:44
  • So basically in summary, if this is confusing for you, you are storing everything in this object called a Hashmap, which groups things by key-value pairs. Then, you're putting all the keys in one arraylist (you are already familiar with arraylists I hope!), and all the values in another arraylist. Finally, you get a random index—and using that index you are able to reference the corresponding key at that index from your first arraylist and the corresponding value at that index from your second arraylist! – Ruchir Baronia Jul 12 '17 at 19:45