HI all, I am having a little problem with the following program. Here are the files:
Player.java:
import java.text.DecimalFormat;
public class Player {
private String name;
private double average;
public Player(String name, double average) {
this.name=name;
this.average=average;
}
public String getName() {
return name;
}
public double getAverage() {
return average;
}
public String getAverageString() {
DecimalFormat decFormat = new DecimalFormat();
decFormat.setMaximumIntegerDigits(0);
decFormat.setMaximumFractionDigits(3);
decFormat.setMinimumFractionDigits(3);
return decFormat.format(average);
}
}
ShowTeamFrame.java:
import java.io.IOException;
class ShowTeamFrame {
public static void main(String args[])
throws IOException {
new TeamFrame();
}
}
TeamFrame.java
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;
@SuppressWarnings("serial")
public class TeamFrame extends JFrame {
public TeamFrame() throws IOException {
Player player;
Scanner keyboard =
new Scanner(new File("Hankees.txt"));
for (int num = 1; num <= 9; num++) {
player = new Player(keyboard.nextLine(),
keyboard.nextDouble());
keyboard.nextLine();
addPlayerInfo(player);
}
setTitle("The Hankees");
setLayout(new GridLayout(9, 2, 20, 3));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void addPlayerInfo(Player player) {
add(new JLabel(" " + player.getName()));
add(new JLabel(player.getAverageString()));
}
}
My problem is with the constructor: where does it get called? I would have thought in this linePlayer player;
but shouldn't this be Player player = new Player();
?
thanks