4

I'm pretty new to Java and in an effort to learn more I'm trying to make a game whereby you're given a random lyric from a song, and then the song title and artist.

This would be pretty easy, however I wanted the lyric to be random, and then print the artist and song title using a seperate method. My problem is that I'm not sure how to go from printing one random string to printing a specific answer.

I currently have the lyrics and the answers in a hashmap with the lyrics as keys and answers as values, and can print random keys by turning them into an arraylist using this method

public void randomLyrics()
    {
        ArrayList<String> lyricKey = new ArrayList<String>(lyrics.keySet());
        if(lyrics.size() > 0) {
            Random rand = new Random();
            int index = rand.nextInt(lyrics.size());
            System.out.println(lyricKey.get(rand.nextInt(lyrics.size())));
        }
    }

however I'm pretty sure that this isn't the right way to go about it, and am not sure how I would go about printing the answers seperately.

Any help would be much appreciated.

Also just to clarify, I want to call a method (randomLyrics) which will display a random lyric such as "that's me in the corner, that's me in the spotlight" and then call a seperate method (answer) which would then print "Losing My Religion - R.E.M."

Many thanks

  • 2
    The first thing you should do would be to model Question-Answer pair as a class with two member variables. That would be exactly the right moment to learn that, this is the simplest possible situation in which it is useful. HashMaps aren't that useful here, as it seems. You could abuse `Map.Entry` to do that, but that's probably not the idea behind the exercise. – Andrey Tyukin Feb 02 '18 at 16:39
  • Hi @AndreyTyukin thanks for your response. Could you elaborate on modeling Question-Answer pair as a class with two member variables as I'm not entirely sure what you mean by that. Cheers. –  Feb 04 '18 at 14:34

1 Answers1

0

Here is full example run this program multiple times you will get some random value:-

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class HashM {

    public static void main(String[] args) {
        HashMap<String, String> lyrics = new HashMap<>();
        // KEY :- Lyrics AND Value is Album Name
        lyrics.put("that's me in the corner, that's me in the spotlight", "Losing My Religion - R.E.M.");
        lyrics.put("I will try not to breathe I can hold my head still with my hands at my knees",
                "Automatic for the People");
        lyrics.put("Eu não aturo mais Sou um trator", "É Duda Brack");

        randomLyrics(lyrics);

    }

    public static void randomLyrics(HashMap<String, String> lyrics) {
        ArrayList<String> lyricKey = new ArrayList<String>(lyrics.keySet());
        if (lyrics.size() > 0) {
            Random rand = new Random();
            int index = rand.nextInt(lyrics.size());
            System.out.println("###::####Your Random Number is :-" + rand.nextInt(lyrics.size()));
            System.out.println(lyricKey.get(rand.nextInt(lyrics.size())));
        }
    }

}
Vipul Gulhane
  • 761
  • 11
  • 16