Celestine Babayaro
Celestine Babayaro

Reputation: 383

How to make joined random between two strings

I have string array name and string array company

public class Main {
    public static void main(String[] args) {
        String[] name = {"Alex", "John", "David", "Peter"};
        String[] company = {"Adidas", "Nike", "New Balance", "Puma"};
        Random random = new Random();
System.out.println(name[random.nextInt(name.length)] + " " + company[random.nextInt(company.length)]);

     }
}

I've got

Alex Puma

That's OK, but I want print all randomed names and companies, such as

Alex Puma
Peter Nike
David New Balance
John Adidas

How to do it best way?

Upvotes: 1

Views: 164

Answers (1)

arshajii
arshajii

Reputation: 129507

Shuffle both arrays:

Collections.shuffle(Arrays.asList(name));
Collections.shuffle(Arrays.asList(company));

Then print each pair:

for (int i = 0; i < name.length; i++)
    System.out.println(name[i] + " " + company[i]);

Upvotes: 2

Related Questions