So basically I've been given a question:
Create program that will read content of members.txt file. Members have to be categorized into 3 arrays, one per team. Once all members are processed, output to the terminal information about how many member each team has and what is name of team captain. File values are separated by comma with following schema:
first_name, last_name, team_name, is_captain
And this is the code that I came up with (idk about efficiency because I'm still fairly new):
package ssst.edu.ba;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner("members.txt");
int rcount = 0, bcount = 0, gcount = 0;
ArrayList<Student> red = new ArrayList<Student>(5);
ArrayList<Student> blue = new ArrayList<Student>(5);
ArrayList<Student> green = new ArrayList<Student>(5);
String line;
String rCap = "";
String bCap = "";
String gCap = "";
while ((line = scan.nextLine()) != null) {
String[] obj = line.split(", ");
if(obj[2] == "Red") {
Student a = new Student(obj[0],obj[1],obj[2],Boolean.parseBoolean(obj[3]));
red.add(a);
rcount++;
if(obj[3] == "true") {
rCap = obj[0] + obj[1];
}
}
else if (obj[2] == "Blue") {
Student a = new Student(obj[0],obj[1],obj[2],Boolean.parseBoolean(obj[3]));
blue.add(a);
bcount++;
if(obj[3] == "true") {
bCap = obj[0] + obj[1];
}
}
else {
Student a = new Student(obj[0],obj[1],obj[2],Boolean.parseBoolean(obj[3]));
green.add(a);
gcount++;
if(obj[3] == "true") {
gCap = obj[0] + obj[1];
}
}
}
System.out.println("There are " + rcount + " people on the red team.");
System.out.println("There are " + bcount + " people on the blue team.");
System.out.println("There are " + gcount + " people on the green team.");
System.out.println("The captain for the red team is: " + rCap);
System.out.println("The captain for the blue team is: " + bCap);
System.out.println("The captain for the green team is: " + gCap);
}
}
The class "Student" is simple, it has 3 private String attributes and a private bool value, it also has one constructor with 4 parameters to set the values to ones given in a text document "members.txt"
The text document is looks like this:
Ginny, Gullatt, Blue, false
Tiara, Curd, Red, false
Camie, Poorman, Green, false
Jammie, Hasson, Green, false
Lionel, Hailey, Blue, false
Genevive, Mckell, Red, true
Esteban, Slaubaugh, Blue, false
Elden, Harte, Red, false
Tasia, Rodrigue, Green, false
Nathanial, Dentler, Red, false
Valda, Nicoletti, Blue, false
Kary, Wilkerson, Green, false
Coletta, Akey, Blue, false
Wilmer, Jack, Red, false
Loreta, Agnew, Green, true
Suzy, Cleveland, Red, false
Pasty, Laprade, Blue, false
Candie, Mehaffey, Green, false
Glady, Landman, Blue, true
Tierra, Mckeown, Red, false
Glady, Laprade, Green, false
Problem: I am receiving this exception message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1
I don't know if this is tied to the ArrayList or to the String array "obj", and I'm also not sure how to fix it.