-2

The user must enter an arbitrary amount of nouns and adjectives in 2 different arrays.(Minimum of 3 per array). Example array A users enters apple, pair, orange. Array b = green, sweet, rotten, blue. Now I need to randomly pick the adjectives and add them to the nouns. Like Sweet apple, rotten pair etc... I can't use the same word twice and I need to use the math.Random(). How can you do this?

public static void main(String[] args) {
    String[] Noun = new String[4];
    String[] Adj = new String[4];
    int numbOfNouns = 0;
    int numbOfAdj = 0;

    Scanner kb = new Scanner(System.in);
    System.out.println("How many nouns ? min 3");
    numbOfNouns = kb.nextInt();

    while (numbOfNouns < 3) {
        System.out.println("How many nouns ? min 3");
        numbOfNouns = kb.nextInt();
    }

    System.out.println("Enter " + numbOfNouns + " nouns");
    for (int i = 0; i <= numbOfNouns; i++) {
        Noun[i] = kb.nextLine();
    }

    System.out.println("How many adjectives ? min 3");
    numbOfAdj = kb.nextInt();


    while (numbOfAdj < 3) {
        System.out.println("How many adjectives ? min 3");
        numbOfAdj = kb.nextInt();
    }

    System.out.println("Enter " + numbOfAdj + " adjectives");

    for (int i = 0; i <= numbOfAdj; i++) {
        Adj[i] = kb.nextLine();
    }

}
Justin L
  • 375
  • 2
  • 10

1 Answers1

2

You can use Collections shuffle method designed for Lists like this:

List<String> arrayNoun = Arrays.asList(Noun);
Collections.shuffle(arrayNoun);

List<String> arrayAdj = Arrays.asList(Adj);
Collections.shuffle(arrayAdj);

By the way, I think you have to fix whole your code like this:

public static void main(String[] args) {
    // when you ask user to enter number of objects in your array then you cannot define fix array size!
    String[] Noun;
    String[] Adj;

    int numbOfNouns = 0;
    int numbOfAdj = 0;

    Scanner kb = new Scanner(System.in);

    // the whole while loop can handle reading the number of nouns and so there is no need to call this code once before the loop!
    while (numbOfNouns < 3) {
        System.out.println("How many nouns ? min 3");
        numbOfNouns = kb.nextInt();
        kb.nextLine(); // get enter key after number enter
    }

    // here you define size of your array according to user input
    Noun = new String[numbOfNouns];

    System.out.println("Enter " + numbOfNouns + " nouns");
    for (int i = 0; i < numbOfNouns; i++) {
        Noun[i] = kb.nextLine();
    }

    while (numbOfAdj < 3) {
        System.out.println("How many adjectives ? min 3");
        numbOfAdj = kb.nextInt();
        kb.nextLine(); // get enter key after number enter
    }

    Adj = new String[numbOfAdj];

    System.out.println("Enter " + numbOfAdj + " adjectives");

    for (int i = 0; i < numbOfAdj; i++) {
        Adj[i] = kb.nextLine();
    }

    List<String> arrayNoun = Arrays.asList(Noun);
    Collections.shuffle(arrayNoun);

    List<String> arrayAdj = Arrays.asList(Adj);
    Collections.shuffle(arrayAdj);
}
Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59