/* * Steve Eilertsen. Divides players into 2 teams based on their birth month. * Odds versus evens. Each team must have 3 boys and 3 girls. * Program allocates a name to each team using a random number. * The text names are in a separate text file. */ // package sixasidebeachtext; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane; public class SixASideBeachText { public static void main(String[] args) { String name,genderString; char gender = 'z'; // char often demands to be initialised to something int month = 0; boolean monthOdd = false, monthEven=false; int teamEvenBoys = 0, teamEvenGirls = 0; int teamOddBoys = 0, teamOddGirls = 0; String [] evenTeamArray = new String[6]; String [] oddTeamArray = new String[6]; int evenTeamCounter = 0; int oddTeamCounter = 0; String [] teamName = new String[20]; // Populate the teamName array. // Reads in a text file, one name per line, no delimiter try { Scanner scFile = new Scanner(new File("SixASideBeachText.txt")); int size = 0; while (scFile.hasNext()) { String line = scFile.nextLine(); teamName[size] = line; size++; } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error! File not found"); } // for debugging purposes for (int i = 0; i < 20; i++) System.out.println(teamName[i]); //INPUT via while loop // Loop executes until the teams are full - equal numbers of boys and girls while (teamEvenBoys < 3||teamEvenGirls < 3||teamOddBoys < 3||teamOddGirls < 3){ name = JOptionPane.showInputDialog("Enter your name"); genderString = JOptionPane.showInputDialog("Enter your gender - M/F"); gender = Character.toUpperCase(genderString.charAt(0)); // PROCESSING. Determin odd or even month of birth. Integer only. month = Integer.parseInt(JOptionPane.showInputDialog( "Enter birth month as a number" + "\n" + "1 - January" + "\n" + "2 - February" + "\n" + "3 - March" + "\n" + "4 - April" + "\n" + "5 - May" + "\n" + "6 - June" + "\n" + "7 - July" + "\n" + "8 - August" + "\n" + "9 - September" + "\n" + "10 - October" + "\n" + "11 - November" + "\n" + "12 - December" + "\n" )); // Determines if the month is even or odd using MOD // There should be erro checking here, ie smaller than 1 and // bigger than 12. if (month % 2 == 0){ monthEven = true; monthOdd = false; } else { monthEven = false; monthOdd = true; } // Allocates into teams based on gender and month of birth // Teams of 6 - 3 boys and 3 girls if (monthOdd) { if (gender == 'M') { if (teamOddBoys < 3 ){ oddTeamArray[oddTeamCounter] = name; teamOddBoys++; oddTeamCounter++; } } else if (gender == 'F') { if (teamOddGirls < 3 ){ oddTeamArray[oddTeamCounter] = name; teamOddGirls++; oddTeamCounter++; } } } // End monthOdd if (monthEven) { if (gender == 'M') { if (teamEvenBoys < 3 ){ evenTeamArray[evenTeamCounter] = name; teamEvenBoys++; evenTeamCounter++; } } else if (gender == 'F'){ if (teamEvenGirls < 3 ){ evenTeamArray[evenTeamCounter] = name; teamEvenGirls++; evenTeamCounter++; } } } // End monthEven monthEven = false; // NB. Reset the monthEven flag for re-use in loop monthOdd = false; // NB. Reset the monthOdd flag for re-use in loop } // end while // Generate 2 random numbers from 1 to 20 inclusive for teamName // Loop does not allow a duplicate name to be chosen int myRandom1 = 0, myRandom2 = 0; while(myRandom1 == myRandom2){ myRandom1 = (int)(Math.random() * (20 + 1) + 1); myRandom2 = (int)(Math.random() * (20 + 1) + 1); } // OUTPUT The even array team with a name from array System.out.println(teamName[myRandom1]); System.out.println("---------"); for (int i = 0; i < 6; i++){ System.out.print(evenTeamArray[i] + " "); } System.out.println(""); System.out.println("==================================="); // OUTPUT The odd array team with a name from the array System.out.println(teamName[myRandom2]); System.out.println("--------"); for (int i = 0; i < 6; i++){ System.out.print(oddTeamArray[i] + " "); } System.out.println(""); } }