I am working on my own personal project where you keep a record of Player objects in a file. I have created a way create a new file and add Player objects to it, or open an existing file and add Player objects. However, to delete a Player object from an existing file, I have to read the Player objects from that file into an ArrayList. Then delete the desired Object, preferably using the remove() method. Once that is done, I write the ArrayList into the file again. This time without the Objects that I removed.
However, I keep getting this error:
HTML Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at roster.Team.delete(Team.java:98)
at roster.Driver.main(Driver.java:26)
I can't get my code to work without solving this error. It would be much appreciated if someone could help me.
Run my code below to see the issue:
HTML Code:
package roster;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws IOException {
Team t1 = new Team();
System.out.println("To create a new roster, type 1");
System.out.println("To add to an existing roster, type 2");
System.out.println("To remove a player from a roster, type 3");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
if(choice == 1){
t1.input();
}
if(choice == 2){
t1.add();
}
if(choice == 3){
t1.delete();
}
scan.close();
}
}
HTML Code:
package roster;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Stream;
import java.io.ObjectInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.FileInputStream;
import java.io.*;
public class Team {
ArrayList<Player> x = new ArrayList<Player>();
public Team(){
this.x = new ArrayList<>();
}
Scanner scan = new Scanner(System.in);
public void input() throws IOException {
int num_people;
System.out.print("Name of file (A file that does not exist will be automatically created): ");
String name;
name = scan.next();
System.out.print("# of people on roster? ");
num_people = scan.nextInt();
System.out.println("Enter first, last, goals, caps, and assists:");
for(int i=0; i<num_people; i++){
String fstname2 = scan.next();
String lstname2 = scan.next();
String goals = scan.next();
String caps = scan.next();
String assists = scan.next();
x.add(new Player(fstname2,lstname2,goals,caps,assists));
}
scan.close();
System.out.println("Done.");
BufferedWriter writer = new BufferedWriter(new FileWriter(name));
for(Player p : x)
if(p != null){
writer.write(p + "\n");
writer.newLine();
}
writer.close();
}
public void add() throws IOException {
System.out.println("Enter the name of the file that you wish to add information to: ");
String name = scan.next();
File w = new File(name);
if(w.exists())
{
System.out.println("How many people would you like to add? ");
int num_people = scan.nextInt();
System.out.println("Enter first, last, goals, caps, and assists:");
for(int i=0; i<num_people; i++){
String fstname2 = scan.next();
String lstname2 = scan.next();
String goals = scan.next();
String caps = scan.next();
String assists = scan.next();
x.add(new Player(fstname2,lstname2,goals,caps,assists));
}
System.out.println("Done.");
BufferedWriter q = new BufferedWriter(new FileWriter(w, true));
for(Player p : x){
q.write(p + "\n");
q.newLine();
}
q.close();
}
else{
System.out.println("File does not exist.");
}
}
public void delete() throws IOException {
System.out.println("Enter the name of the file that you wish to delete information from: ");
String name = scan.next();
File w = new File(name);
if(w.exists())
{
try {
Scanner sc = new Scanner(w);
ArrayList<Player> x = new ArrayList<Player>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = new String[4];
details = line.split(" ");
String fstname = details[0];
String lstname = details[1];
String goals = details[2];
String caps = details[3];
String assists = details[4];
//int age = Integer.parseInt(details[2]);
Player p = new Player(fstname,lstname,goals,caps,assists);
x.add(p);
}
for(Player p: x){
System.out.println(p.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("How many people would you like to remove? ");
int num_people = scan.nextInt();
System.out.println("Enter first, last, goals, caps, and assists:");
for(int i=0; i<num_people; i++){
String fstname2 = scan.next();
String lstname2 = scan.next();
String goals = scan.next();
String caps = scan.next();
String assists = scan.next();
x.remove(new Player(fstname2,lstname2,goals,caps,assists));
}
System.out.println("Done.");
System.out.println();
for(int i=0;i<x.size();i++){
System.out.println(x.get(i));
}
BufferedWriter q = new BufferedWriter(new FileWriter(w)); //The Object I "deleted" still shows up in file
for(Player p : x){
q.write(p + "\n");
q.newLine();
}
q.close();
}
else{
System.out.println("File does not exist.");
}
}
HTML Code:
package roster;
public class Player {
String fstname, lstname, goals, caps, assists;
public Player(String fstname, String lstname, String goals_, String caps_, String assists_) {
super();
this.fstname = fstname;
this.lstname = lstname;
this.goals = goals_;
this.caps = caps_;
this.assists = assists_;
}
public String toString(){
return fstname + " " + lstname + " " + goals + " " + caps + " " + assists;
}
public String getFstname() {
return fstname;
}
public void setFstname(String fstname) {
this.fstname = fstname;
}
public String getLstname() {
return lstname;
}
public void setLstname(String lstname) {
this.lstname = lstname;
}
public String getGoals() {
return goals;
}
public void setGoals(String goals) {
this.goals = goals;
}
public String getCaps() {
return caps;
}
public void setCaps(String caps) {
this.caps = caps;
}
public String getAssists() {
return assists;
}
public void setAssists(String assists) {
this.assists = assists;
}
}