I am trying to make an input dialog that prompts the user to select a second team that plays the first team. The team the user selected before cannot show again and there needs to be an error check if the user inputs something that isn't a team option. I am using a class called Team to get the team names from a data file. I have already created the method to select the first team. This is the desired output: The message for the user to select two teams
I made a while loop that keeps running if the user inputs the first team again or something that wasn't a team option. I made a different JOptionPane Input Dialog for each team and thought that the user could input anything and the code would check if its valid. **However, when the program runs, it skips the method entirely. **
*League * is the array of teams with wins and losses and their roster (players). This method has two formal parameters that accepts the league array and the user's first team selection called team1. This method is supposed to return the second team selection.
This is my code currently:
for (int i = 0; i < league.length; i++) {
while (!team2.equalsIgnoreCase(league[i].getName()) && team2.equals(team1)) {
if (team1.equals(league[0].getName())) {
display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
+ "-" + league[1].getName() + "\n"
+ "-" + league[2].getName() + "\n"
+ "-" + league[3].getName() + "\n\n");
} else if (team1.equals(league[1].getName())) {
display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
+ "-" + league[0].getName() + "\n"
+ "-" + league[2].getName() + "\n"
+ "-" + league[3].getName() + "\n\n");
} else if (team1.equals(league[2].getName())) {
display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
+ "-" + league[0].getName() + "\n"
+ "-" + league[1].getName() + "\n"
+ "-" + league[3].getName() + "\n\n");
} else if (team1.equals(league[3].getName())) {
display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
+ "-" + league[0].getName() + "\n"
+ "-" + league[1].getName() + "\n"
+ "-" + league[2].getName() + "\n\n");
} else {
popup("Sorry, I couldn't find " + team2 + ", please try again...");
}
}
}
Any help would be great!
You may instead want to try something a little different. Perhaps use a couple of JComboBoxes to hold team names. When team name is selected from the first Combobox then drop that name from the second Combobox. The second ComboBox won't even become active until the first Combo has a selected name. The OK button won't become enabled until the second ComboBox has a selected name. This is demonstrated within the below GIF:
Here is the runnable code. Be sure to read all the comments in code: