When I run this program, which is getting it\'s input from Teams.txt, it returns
ID: 3621724 • Letter: W
Question
When I run this program, which is getting it's input from Teams.txt, it returns the error :Exception in thread "main" java.lang.NumberFormatException: For input string: "NCSU 28"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at Tournament.main(Tournament.java:25)
after I enter the number of teams.
The program is in 2 parts as follows:
import java.io.*;
public class Tournament {
// Set this to wherever your team file is located
final static String TEAMFILE = "Teams.txt";
public static void main(String[] args) throws IOException {
System.out.print("Enter the number of teams: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int numTeams = Integer.parseInt(in.readLine());
BufferedReader reader = new BufferedReader(new FileReader(TEAMFILE));
Team allTeams [] = new Team[numTeams];
for (int i=0; i<numTeams; i++) {
/* Question didn't specify format of input text file.
* I'm assuming that team names and wins are on separate lines
* with each team name followed by the team's wins.
* You'll need to modify this block if the input is formatted differently.
*/
String team = reader.readLine();
int wins = Integer.parseInt(reader.readLine());
allTeams[i] = new Team(team,wins);
}
sort(allTeams);
int gameNumber = 1;
for (int i = 0, j = allTeams.length - 1; i < j; i++, j--)
{
System.out.println("Game " + gameNumber + ": " + allTeams[ i ].getName() + " vs. " + allTeams[ j ].getName());
gameNumber++;
}
}
// Sorts teams using selection sort
private static void sort(Team[] teams) {
for (int i=0; i<teams.length; i++) {
int worstTeamIndex = i;
for (int j = i; j<teams.length; j++) {
if (teams[j].getWins() < (teams[worstTeamIndex].getWins())) {
worstTeamIndex = j;
}
}
Team tmp = teams[worstTeamIndex];
teams[worstTeamIndex] = teams[i];
teams[i] = tmp;
}
}
}
public class Team
{
private String name;
private int wins;
public Team(String name, int wins) {
this.name = name;
this.wins = wins;
}
public Team() {
this.name = "";
this.wins = 0;
}
public String getName() {
return name;
}
public int getWins() {
return wins;
}
public void incrementWins() {
wins++;
}
}
THE CONTENTS OF TEAM.TXT FILE IS :
UNCC 30
NCSU 28
GT 25
USC 20
GSU 19
UVA 15
UGA 2
UNC 1
iM TRYING TO USE AN ARRAY AND GET THE OUTPUT TO LOOK LIKE:
Game 1: UNCC vs. UNC
Game 2: NCSU vs. UGA
Game 3: GT vs. UVA
Game 4: USC vs. GSU
Explanation / Answer
my baad
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.