I keep getting an out of bounds exception error when adding a second Entry variable.
The file that I’m trying to read in has a last line that just says "END"
the rest of the lines are formatted such as
1839,Sutter's Fort founded
and the file ends with:
END
while (myReader.hasNextLine()) {
String string = myReader.nextLine();
if (string == "END") break;
String[] parts = string.split(",");
String part1 = parts[0];
String part2 = parts[1];
Entry a = new Entry(part1, part2);
array[10].addHead(a); // printing out the values
System.out.println(part1 + " " + part2);
System.out.println(array[10].removeHead().key);
}
I tried adding a break statement if the String were to equal "END" but it seems as though it either won’t break out or just ignores it.
>Solution :
You need to use String.equals str == otherStr only returns true if both sides are the exact same object
while (myReader.hasNextLine()) {
String string = myReader.nextLine();
if (string.equals("END")) break;
String[] parts = string.split(",");
String part1 = parts[0];
String part2 = parts[1];
Entry a = new Entry(part1, part2);
array[10].addHead(a); // printing out the values
System.out.println(part1 + " " + part2);
System.out.println(array[10].removeHead().key);
}