I'm working on a recursion project for myself, and even though this program was working previously, it stopped working and gives an ArrayIndexOutOfBounds error on line 161. I'm pretty confused at the moment, and it would be incredibly helpful if any of you guys could help.
Here's the code so far, although it's kinda long --
Thanks in advance!
import java.nio.file.*;
import java.io.*;
import java.util.*;
import java.lang.Object;
public class KevinBacon {
public static void options() {
System.out.println("Select Options (Enter Q to Quit)");
System.out.println("1. Bacon Degrees of Freedom");
System.out.println("2. Add Entry");
System.out.println("3. Delete Entry");
}
public static String tabs(int degrees) {
String tab = "";
for (int i = 0; i < degrees; i++) {
tab = tab.concat("\t");
}
return tab;
}
//Recursion
public static int DOF(String mainActorNames, String names, String masterList[][], int degrees) {
degrees++;
String temp[][] = masterList;
ArrayList<String> movieList = getMovieList(names, temp);
if (movieList.size() > 0) {
for (int i = 0; i < movieList.size(); i++) {
if (temp.length > 0) {
ArrayList<String> actorList = new ArrayList<String>();
for (int j = 0; j < temp.length; j++) {
if (temp[j][1].contains(movieList.get(i))) {
actorList.add(temp[j][0]);
}
}
for (int kb = 0; kb < actorList.size(); kb++) {
if (actorList.get(kb).contains("Kevin Bacon")) {
System.out.println("Recursion level: " + degrees + "\n" + tabs(degrees) + names
+ " acted with " + actorList.get(kb) + "in movie " + movieList.get(i));
System.out.println(tabs(degrees) + mainActorNames + " has Bacon Number " + degrees + "\n");
break;
}
else {
if (!actorList.get(kb).contains(names)) {
System.out.println("Recursion level: " + degrees + "\n" + tabs(degrees) + names
+ " acted with " + actorList.get(kb) + "in movie " + movieList.get(i));
DOF(mainActorNames, actorList.get(kb), delTempArr(movieList.get(i), temp), degrees);
}
else if (!actorList.get(kb).contains(names)) {
//placeholder
}
}
}
}
}
}
else {
System.out.println(tabs(degrees - 1) + "No Movie Available in Database for " + names);
}
return 0;
}
//adds movies to temp array
private static ArrayList<String> getMovieList(String actor, String temp[][]) {
ArrayList<String> movieList = new ArrayList<>();
for (int i = 0; i < temp.length; i++)
if (temp[i][0].contains(actor)) {
movieList.add(temp[i][1]);
}
return movieList;
}
//Remove scanned movies and stuff from the temp array
public static String[][] delTempArr(String movieNames, String temp[][]) {
String temp2[][] = new String[temp.length-2][3];
int count = 0;
for (int j = 0; j < temp.length; j++) {
if (!temp[j][1].contains(movieNames)) {
temp2[count][0] = temp[j][0];
temp2[count][1] = temp[j][1];
temp2[count][2] = temp[j][2];
count++;
}
}
return temp2;
}
//Welcome to the main method
public static void main(String[] args) throws IOException {
//welcome to my variables, please look at my wares
Scanner input = new Scanner(System.in);
Scanner file = new Scanner(new File("list.txt"));
Scanner file1 = new Scanner(new File("list.txt"));
int degrees = 1;
ArrayList<String> kevMovies = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
ArrayList<String> allMovies = new ArrayList<>();
ArrayList<String> years = new ArrayList<>();
int counter = 0;
String line;
String movieName;
String actorName;
String year;
boolean bool;
boolean bool1;
//determines size of the arr
BufferedReader read = new BufferedReader(new FileReader("list.txt"));
counter = 0;
while (read.readLine() != null) {
counter++;
}
read.close();
//welcome to the big boy array
String theBigOne[][] = new String[counter][3];
for (int i = 0; i < counter; i++) {
line = file.nextLine();
String[] sploit = line.split(" ");
actorName = sploit[0] + sploit[1];
movieName = "";
year = sploit[sploit.length - 1];
for (int j = 2; j < sploit.length - 1; j++) {
movieName = movieName + sploit[j] + " ";
}
theBigOne[i][0] = actorName;
theBigOne[i][1] = movieName;
theBigOne[i][2] = year;
names.add(actorName);
allMovies.add(movieName);
years.add(year);
}
String poob = "";
//While loop :(((
while (!poob.contentEquals("Q")) {
FileWriter filewriter = new FileWriter("list.txt", true);
Formatter output = new Formatter(filewriter);
options();
poob = input.nextLine();
if (poob.contentEquals("1") || poob.contentEquals("2") || poob.contentEquals("3")) {
int option = Integer.parseInt(poob);
//Bacon Number
if (option == 1) {
bool=true;
degrees = 0;
System.out.println("Enter Actor Name:");
String nameOfActor = input.nextLine();
String mainActorNames = nameOfActor;
//Check the bounds!
String[] split3 = nameOfActor.split(" ");
//Number restriction
for (char c : nameOfActor.toCharArray()) {
if (Character.isDigit(c)) {
bool = false;
}
}
if (split3.length == 2 && bool) {
ArrayList<String> summary = new ArrayList<String>();
DOF(mainActorNames, nameOfActor, theBigOne, degrees);
}
else {
System.out.println("Invalid input. Try again.");
bool = true;
}
}
//the second option
else if (option == 2) {
System.out.println("Enter Actor Name:");
String inputName = input.nextLine();
//Bounds
String[] split1 = inputName.split(" ");
bool = true;
for (char c : inputName.toCharArray()) {
if (Character.isDigit(c)) {
bool = false;
}
}
if (split1.length == 2 && bool) {
System.out.println("Enter movie name:");
String movieInput = input.nextLine();
System.out.println("Enter year:");
String yearInput = input.next();
//Bounds
String[] split2 = yearInput.split(" ");
bool1 = true;
for (char c : yearInput.toCharArray()) {
if (Character.isLetter(c)) {
bool1 = false;
}
}
if (bool1 && split2.length == 1) {
output.format("\n%s %s %s", inputName, movieInput, yearInput);
output.close();
bool = true;
bool1 = true;
}
else {
System.out.println("Invalid input. Try again.");
bool = true;
bool1 = true;
}
}
}
//the third and final option
else if (option == 3) {
System.out.println("Enter Actor Name:");
String inputtedName = input.nextLine();
//Bounds
String[] split6 = inputtedName.split(" ");
bool = true;
for (char c : inputtedName.toCharArray()) {
if (Character.isDigit(c)) {
bool = false;
}
}
if (split6.length == 2 && bool) {
System.out.println("Enter movie name:");
String movieInput = input.nextLine();
System.out.println("Enter year:");
String yearInput = input.next();
//Bounds
String[] split2 = yearInput.split(" ");
bool1 = true;
for (char c : yearInput.toCharArray()) {
if (Character.isLetter(c)) {
bool1 = false;
}
}
if (bool1 && split2.length == 1) {
String del = inputtedName + " " + movieInput + " " + yearInput;
String currDir = System.getProperty("user.dir");
Path path = Paths.get(currDir + "/list.txt");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll(del, "");
Files.write(path, content.getBytes(charset));
}
else {
System.out.println("Invalid input. Try again.");
bool = true;
bool1 = true;
}
}
else {
System.out.println("Invalid input. Try again.");
bool = true;
bool1 = true;
}
}
else {
System.out.println("Invalid input. Try again.");
bool = true;
bool1 = true;
}
}
}
}
}```