dimanche 24 mai 2020

Java Random generate string from array preventing duplicates

Sorry i am very new to java but i am having trouble finding a clear answer to fix the problem with my code. I am making a mad lib game that randomly generates nouns, adjectives, verbs, etc, and fills in the blanks in the story. I am finding that the code generates the same words over and over as I have multiple adjectives in the story. I am not sure how to fix this so there are no duplicates without having to change a lot of what i have already worked on. FYI i am aware my story doesn't have a spot for verbs yet. Heres my code..

public static void main(String[] args) throws FileNotFoundException {

 Scanner inputNouns = new Scanner(new File("nouns"));
ArrayList<String> nouns = new ArrayList<>();
while(inputNouns.hasNextLine())
    nouns.add(inputNouns.nextLine());
Random rNouns = new Random();

int randomNoun = rNouns.nextInt(nouns.size());
String randomNounWord = nouns.get(randomNoun);

Scanner inputVerbs = new Scanner(new File("verbs"));
ArrayList<String> verbs = new ArrayList<>();
while(inputVerbs.hasNextLine())
    verbs.add(inputVerbs.nextLine());
Random rVerbs = new Random();

int randomVerb = rVerbs.nextInt(verbs.size());
String randomVerbWord = verbs.get(randomVerb);

Scanner inputAdjectives = new Scanner(new File("adjectives"));
 ArrayList<String> adjectives = new ArrayList<>();
 while(inputAdjectives.hasNextLine())
    adjectives.add(inputAdjectives.nextLine());
 Random rAdjectives = new Random();

  int randomAdjective = rAdjectives.nextInt(adjectives.size());
String randomAdjectiveWord = adjectives.get(randomAdjective);



 Scanner inputNames = new Scanner(new File("names"));
 ArrayList<String> names = new ArrayList<>();
 while(inputNames.hasNextLine())
    names.add(inputNames.nextLine());
 Random rNames = new Random();

int randomName = rNames.nextInt(names.size());
String randomNameWord = names.get(randomName);

 Scanner inputPlaces = new Scanner(new File("places"));
 ArrayList<String> places = new ArrayList<>();
 while(inputPlaces.hasNextLine())
    places.add(inputPlaces.nextLine());
 Random rPlaces = new Random();

int randomPlace = rPlaces.nextInt(places.size());
String randomPlaceWord = places.get(randomPlace);




  System.out.println("Ladies and gentlemen, on this " + randomAdjectiveWord + " occasion");
   System.out.println("It is a privilege to address such a/an " + randomAdjectiveWord +" looking group of " +randomNounWord);
   System.out.println("I can tell from your smiling " +randomNounWord + " that you will support my");
   System.out.println(randomAdjectiveWord + " program in the coming election. I promise that, if elected,");
   System.out.println("there will be a/an " + randomNounWord + " in every " + randomNounWord+ ". I want to warn you aganist");
   System.out.println(randomAdjectiveWord + " opponent, " + randomNameWord +". They are nothing but a/an " +randomAdjectiveWord + " " +randomNounWord);
   System.out.println("They have a/an " +randomAdjectiveWord+ " character and is working "+randomNounWord+" in glove");
   System.out.println("with the criminal element. If elected for the offices of "+randomPlaceWord);
   System.out.println("I will eliminate the " +randomNounWord+ " off the streets. Thank you, and goodnight!");

}

}




Aucun commentaire:

Enregistrer un commentaire