gpt4 book ai didi

java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 0 at proj5. main(proj5.java:15)

转载 作者:行者123 更新时间:2023-12-01 18:39:38 26 4
gpt4 key购买 nike

编译程序时出现此错误。

线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 proj5.main(proj5.java:15)

当我双击错误时,它会将我定向到“String origin = args[0];”

这是完整的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class proj5 {

public static void main(String[] args) {
//validates command line
if(args == null || args.length != 6){
System.out.println("Invalid command line arguments");
System.out.println("Usage: Project5 [origin] [minSpeed] [maxSpeed] [maxBoxcars] [inputFile] [outputFile]");
}

//gets arguments
String origin = args[0];
int minSpeed = Integer.parseInt(args[1]);
int maxSpeed = Integer.parseInt(args[2]);
int maxBoxcars = Integer.parseInt(args[3]);
String inputFile = args[4];
String logFile = args[5];

//create new train log
TLog log = new TLog(logFile);
try {
log.createFile();
}catch(Exception ex){
System.out.println("Failed to create output file");
}

//create train
Train train = new Train(origin, minSpeed, maxSpeed, maxBoxcars, log);
//read file
readFile(inputFile, train);
}

private static void readFile(String inputFile, Train train){
boolean quit = false;

BufferedReader br = null;

try {
String line;

//reader for file
br = new BufferedReader(new FileReader(inputFile));
//loop until end of file or quit
while (!quit && ((line = br.readLine()) != null)) {
//get commands
if(line.equals("PRINT")){
//PRINT
train.logCommand("PRINT", new String[]{});
train.printStatus();
} else if(line.equals("ARRIVE")){//ARRIVE
train.logCommand("ARRIVE", new String[]{});
train.setArrived();
} else if(line.equals("DEPART")){//DEPART
String city = br.readLine();
train.logCommand("DEPART", new String[]{city});

train.setDeparted(city);
}else if(line.equals("SPEEDUP")){//SPEEDUP
int mph = Integer.parseInt( br.readLine());
train.logCommand("SPEEDUP", new String[]{String.valueOf(mph)});
train.speedUp(mph);
}else if(line.equals("SLOWDOWN")){//SLOWDOWN
int mph = Integer.parseInt(br.readLine());
train.logCommand("SLOWDOWN", new String[]{String.valueOf(mph)});
train.slowDown(mph);
}else if(line.equals("ADDCAR")){//ADDCAR
String type = br.readLine();
int maxElements = Integer.parseInt(br.readLine());
BoxCar boxCar = null;
//Create new boxcar based on type
if(type.equals("PERSON")){
boxCar = new PersonCar(maxElements);
}else if(type.equals("CARGO")){
boxCar = new Car(maxElements);
}

train.logCommand("ADDCAR", new String[]{type, String.valueOf(maxElements)});
train.addCar(boxCar);

}else if(line.equals("REMOVECAR")){ //REMOVECAR
int carNum = Integer.parseInt(br.readLine());
train.logCommand("REMOVECAR", new String[]{String.valueOf(carNum)});
train.removeCar(carNum);
}else if(line.equals("LOAD")){//LOAD
String cargoType = br.readLine();
int boxCarId = Integer.parseInt(br.readLine());
String id = br.readLine();

CarContents carContents = null;
//create contents based upon cargo type
if(cargoType.equals("PERSON")){
String name = br.readLine();
int age = Integer.parseInt( br.readLine());
carContents = new Person(id, name, age);
train.logCommand("LOAD", new String[]{String.valueOf(boxCarId), id, name, String.valueOf(age)});
} else if(cargoType.equals("CARGO")){
int weight = Integer.parseInt(br.readLine());
int height = Integer.parseInt(br.readLine());
int width = Integer.parseInt(br.readLine());
int length = Integer.parseInt(br.readLine());
carContents = new Cargo(id, weight, height, width, length);
train.logCommand("LOAD", new String[]{String.valueOf(boxCarId), String.valueOf(weight), String.valueOf(height),
String.valueOf(width), String.valueOf(length)});
}

train.load(boxCarId, carContents);

}else if(line.equals("UNLOAD")){//UNLOAD
int boxCarId = Integer.parseInt(br.readLine());
String cargoId = br.readLine();
train.logCommand("UNLOAD", new String[]{String.valueOf(boxCarId), cargoId});
train.unload(boxCarId, cargoId);

}else if(line.equals("QUIT")){//QUIT

train.logCommand("QUIT", new String[]{});
train.logMessage("Quitting...");
quit = true;
}
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}

如果您需要其他类,请告诉我,我有 BoxCar.java、Car.java、CarContents.java、CarException.java、Cargo.java、Person.java、PersonCar.java、TLog.java 和 Train .java。

这是我遇到的唯一错误,它阻止我编译和运行程序。

最佳答案

如果错误在行:

String origin = args[0];

那么这意味着您没有向程序提供任何运行时参数。

尽管您检查了参数,但您没有采取任何纠正措施,只是在 if block 之后继续执行。您可能需要将其余代码移至 else block 中以避免 ArrayIndexOutOfBoundException:

        //validates command line
if(args == null || args.length != 6){
System.out.println("Invalid command line arguments");
System.out.println("Usage: Project5 [origin] [minSpeed] [maxSpeed] [maxBoxcars] [inputFile] [outputFile]");
} else {
//rest of the code
}

关于java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 0 at proj5. main(proj5.java:15),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com