Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWri

ID: 3628283 • Letter: I

Question

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class CarGarageTester {

public static void main(String[] args) throws Exception {

if (args.length < 2) {
throw new Exception("Garage.txt");
}

try {
if (args.length < 2) {
throw new Exception("Garage.txt");
}

} catch (Exception e) {
System.out.println("Unable to find " + ": " + e.getMessage());
}

Garage g = new Garage(10);

PrintWriter pw = new PrintWriter(new FileWriter(args[1]));//this is line 35
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String> int lineCount = 1;
while (oneRecord != null) {
String tokens[] = oneRecord.split(" ");
if (tokens.length != 2) {
System.err.println("Invalid record at line(" + lineCount + "): "" + oneRecord + """);
}

String license = tokens[0];
boolean arrival = tokens[1].equals("ARRIVE");
if (arrival) {
pw.print(oneRecord);
pw.print(": ");
boolean didArrive = g.arrive(license);
if (didArrive) {
pw.println("parked");
} else {
pw.println("turned away");
}
} else {
pw.print(oneRecord);
pw.print(": ");
Car c = g.depart(license);
if (c == null) {
pw.println("not in the garage");
} else {
pw.println("departs after having moved " + c.getNumberOfMoves() + " time(s).");
}
}
> lineCount++;
}
pw.flush();

this is the error i get:
Unable to create : Garage.txt
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at CarGarageTester.main(CarGarageTester.java:35)
Java Result: 1

Explanation / Answer

your error is coming at the line that you pointed out: PrintWriter pw = new PrintWriter(new FileWriter(args[1]));//this is line 35 because args doesn't contain two arguments. The code that you pasted has two instances of the args check, so I'm doubting that you tested against the source that you pasted. Please be sure that you're rebuilding and calling the correct code. The troubling part about the second args check (where you have the try/catch) is that you catch the exception, print an error, and continue. This seems like a fatal error which should end with a System.exit or a return call. Hope this helps.