Ceri Westcott
Ceri Westcott

Reputation: 300

How to turn text and a value from an array into a String?

Basically the program is pretty simple:

it takes a list of names and makes each player verse every player but only once..

So ceri would play 5 games in a row but what I want to happen is it to be random..

public class hatpicking {


    public static void main(String[] args) {
    String[] Names = { "Ceri", "Matthew", "Harry", "Lewis", "Kwok", "James"};


    List<String> Matches = new ArrayList<String>();

    for(int i = 0; i < Names.length; i++){
        for(int j = i + 1; j <Names.length; j++){
            if(Names[i].equals(Names[j])){
                continue;
            }else{

                Matches.add(Names[i] + " v" Names[j]);
                    System.out.println(Names[i] + " v " + Names[j]);
                }
            }   
        }       
    }       
}

I'm sure there is an easier way to randomise stuff but i'm only getting back into Programming so I need the work where ever I can...

Pretty much I want to assign: (Names[i] + " v " Names[j]); to the ArrayList - Matches but obviously

                Matches.add(Names[i] + " v" Names[j]);

does not work, any hints?

Upvotes: 0

Views: 51

Answers (3)

javapadawan
javapadawan

Reputation: 907

My guess is you just want to randomize the matches. So just use the existing code you have and just shuffle the output.

For for best shuffling results use see Shuffling section in this link. http://bost.ocks.org/mike/algorithms/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RandomMatch {

  public void matchGenerator() { 
      List<String> availablePlayers = Arrays.asList("Ceri", "Matthew", "Harry", "Lewis", "Kwok", "James");
      List<String> matches = new ArrayList<String>();
      int MAX_PLAYERS = availablePlayers.size();
      for(int i=0; i<MAX_PLAYERS; i++) {
          String homePlayer = availablePlayers.get(i);
          for(int j=i+1; j<MAX_PLAYERS; j++) {
              String awayPlayer = availablePlayers.get(j);
              if(!homePlayer.equals(awayPlayer)) {
                  String match = homePlayer + " vs. " + awayPlayer;
                  matches.add(match);   
              }
          }
      }
      System.out.println("Match List\n");
      for(String match : matches) 
          System.out.println(match);
      shuffle(matches);
      System.out.println("Shuffled Match List\n");
      for(String match : matches) 
          System.out.println(match);

  }

  public void shuffle(List<String> matches) { 
      long seed = System.currentTimeMillis();
      Collections.shuffle(matches, new Random(seed));
      seed = System.currentTimeMillis();
      Collections.shuffle(matches, new Random(seed));
  }


  public static void main(String[] args) {
      RandomMatch randomMatch = new RandomMatch();
      randomMatch.matchGenerator();
  }
}

Upvotes: 0

Zach
Zach

Reputation: 4792

Eran's answer is correct and will fix your bug. However, on a side note, a word about Java naming conventions. In Java, class names should always start with a capital letter, so class hatpicking should be class Hatpicking. In addition, variable names should start with a lowercase letter, so Names and Matchesshould be names and matches.

Upvotes: 1

Eran
Eran

Reputation: 393791

Matches.add(Names[i] + " v" Names[j]);

should be

Matches.add(Names[i] + " v" + Names[j]);

Upvotes: 2

Related Questions