I have been trying to get the objects in my array to print out fully instead of what I think is their address in memory? I had a for each loop that printed out all of the elements, and it worked however, I really only need the top 3. So I chose to copy the top three elements to a second array to iterate through and print. instead of printing the top 3 it just displays a long list of addresses. this happens every time I run option to or three, and I can only run them after running option 1 once. Would someone, please instruct me on what I'm doing wrong?
// Imports
import java.util.*;
import java.io.*;
class Player{
public String first_name;
public String last_name;
public double attack; // attack stats
public double block; // block stats
// Creation of a constructor for player object. it takes a line which will be passed from scanner a later on.
public Player(String first_name ,String last_name , double attack, double block){
this.first_name = first_name;
this.last_name = last_name;
this.attack = attack;
this.block = block;
}
}
class VerdeVolleyball{
// an array to hold all players
static Player[] roster = new Player[20];
// number of players on roster
static int players_on_roster;
// METHODS
// 1) Open a roster, then the program should ask for the filename of the roster, then read the data from that file into an array.
public static void openRoster(){
// prompt for roster name
try{
Scanner input = new Scanner(System.in);
System.out.println("Enter the roster:");
String entry = input.nextLine();
File file = new File(entry);
// Scanner object to read each players info
Scanner file_scanner = new Scanner(file);
int index = 0;
String line = null;
while((file_scanner.hasNextLine())){
//as long as it has a line set it to line and create a player from that line
line = file_scanner.nextLine();
String[] split = line.split(" ");
String first_name = split[0];
String last_name = split[1];
double attack = Double.parseDouble(split[2]);
double block = Double.parseDouble(split[3]);
Player player = new Player(first_name, last_name, attack, block);
roster[index]= player;
index++;
players_on_roster++;
}
file_scanner.close();
System.out.println(players_on_roster + " players on roster.");
}catch(FileNotFoundException e) {
System.out.println("File not found!");
e.printStackTrace();
}catch(NullPointerException e) {
System.out.print("NullPointerException Caught!");
}
}
//2) List top 3 attackers, then the program should determine and list the names and stats of the players with the top 3 attack stats.If the user tries to list the top attackers without first opening a roster file, then the program should respond with the error message "Please open a roster."
public static void listTopAttackers(){
try{
for(int sort_iters = 0; sort_iters < players_on_roster; sort_iters++){
for(int index = sort_iters+1; index < players_on_roster; index++){
if(roster[index].attack < roster[index+1].attack){
// Swap to order
Player temp_holder = roster[index];
roster[index] = roster[index+1];
roster[index+1] = temp_holder;
// create an array to store top 3
Player[] top_3_attack = new Player[3];
top_3_attack[0] = roster[0];
top_3_attack[1] = roster[1];
top_3_attack[2] = roster[2];
for(Object p : top_3_attack) {
Player player = (Player) p;
System.out.println(top_3_attack.toString());
}
}
}
}
}catch(NullPointerException e) {
System.out.print("NullPointerException Caught!");
}
}
//3) List top 3 blockers, then the program should determine and list the names and stats of the players with the top 3 stats for blocks.If the user tries to list the top blockers without first opening a roster file, then the program should respond with the error message "Please open a roster."
public static void listTopBlockers(){
try{
for(int sort_iters = 0;sort_iters < players_on_roster; sort_iters++){
for(int index = sort_iters + 1; index < players_on_roster; index++){
if(roster[index].block < roster[index+1].block){
// Swap to order
Player temp_holder = roster[index];
roster[index] = roster[index+1];
roster[index+1] = temp_holder;
// create an array to store top 3
Player[] top_3_block = new Player[3];
top_3_block[0] = roster[0];
top_3_block[1] = roster[1];
top_3_block[2] = roster[2];
for(Object p : top_3_block) {
Player player = (Player) p;
System.out.println(top_3_block.toString());
}
}
}
}
}catch(NullPointerException e) {
System.out.print("NullPointerException Caught!");
}
}
//4) Make and display scrimmage teams, then the program should divide the roster into 6-person scrimmage teams, and display the list of players on each team.If the user tries to make and display scrimmage teams without first opening a roster file, then the program should respond with the error message "Please open a roster.
/* public static void createTeams(Player p[],int count){
// determined the number teams, with and without odd players
remainder = roster.length % 6;
int teams = (roster.length - remainder) / 6;
//Array to store the team names
String scrimmage_teams[] = new String[teams];
for(int i = 0; i < teams; ir++){
Scrimmage_teams[i = new String();
}
listTopAttackers();
for(int i = 0; i <teams;i++){
int attackers = 0;
int j = i;
while(attackers <3 && j < roster.length){
scrimmage_teams[i] = scrimmag_teams[i] + "\t" + roster[j].name
}
}
} */
public static void main(String[] args){
Scanner selection_input = new Scanner(System.in);
String user_selection = null;
do{
//Menu
System.out.println("");
System.out.println("1) Open a roster");
System.out.println("2) List top 3 attackers");
System.out.println("3) List top 3 blockers");
System.out.println("4) Make and display scrimmage teams");
System.out.println("5) Quit");
System.out.println("");
//Read user option
user_selection = selection_input.nextLine();
if ( user_selection.equals("1")){
openRoster();
} else if (user_selection.equals("2")){
listTopAttackers();
} else if (user_selection.equals("3")){
listTopBlockers();
//}else if (user_selection.equals("4")){
// makeTeams(); */
} else if (user_selection.equals("5")){
System.out.println("Quitting Program");
System. exit(0);
} else{
System.out.println("Please choose from the given options");
}
}while(true);
}
}// class end