有人可以帮我弄清楚我遇到的这个问题。只有在我们知道数组中没有列出某个选项后,我才需要激活throw。例如,如果用户输入9 in for Powertrain,并且只有2个选项可用。
列表示例: 动力总成 1自动7500.00 2手册5000.00 座位 1多点800.00 2加热1200.00 3 Air 2000.00
它不是检查两个索引,而是两次调用Execption方法。这是我面临的问题的一个例子。 9是动力总成选项所在的位置所以我认为它会检查动力系统选项1和2,然后意识到它不在那里但是它似乎在每次检查后停止。我非常擅长使用Exceptions,所以任何帮助都表示赞赏。谢谢:))
产品识别码: 110111911111 OptionsNotFoundException:可用选项为Automatic OptionsNotFoundException:可用的选项是Manual
更新代码:
public void setOptions(String o) throws IOException {
options = new String[]{"Model", "Exterior Color", "Interior Color", "Powertrain", "Seat", "Radio", "Tire", "Rim", "Miscellaneous"};
//ArrayList to hold the read .txt file
ArrayList<String> textFile = readConfig(config);//new ArrayList<>();
//Attempts to catch format error
try {
//Calls pidFormat method, then stores the returned array in pIDFormat
int[] pIDFormat = pidFormat(o.charAt(0));
if (((o.charAt(0) == '1') && (o.length() == 12)) || ((o.charAt(0) == '2') && o.length() == 15) || ((o.charAt(0) == '3') && o.length() == 15)) {
//Converts pID to formatted style then stores in formattedPID array
formattedPID = new String[pIDFormat.length];
int current = 0;
for (int i = 0; i < pIDFormat.length; i++) {
formattedPID[i] = o.substring(current, pIDFormat[i]);
current = pIDFormat[i];
}
} else {
throw new MakeFormatException(pIDFormat[8]);
}
} catch (MakeFormatException e) {
System.out.println(e);
AnyCarManufacturer.request();
}
//Adds the word model to the first index of the arraylist
textFile.add(0, "Model");
int index = 0;
boolean obj = false;
String[] outOfBounds = new String[80];
String optionsOutOfBounds = null;
String optionsOutOfBoundsIndex;
ArrayList<String>outofBoundsIndex=new ArrayList<>();
for (int i = 0; i < textFile.size() - 1; i++) {
if (!obj) {
if (index == options.length) {
break;
} else if (textFile.get(i).equalsIgnoreCase(options[index])) {
obj = true;
}
} else {
if (index + 1 < options.length && textFile.get(i).equalsIgnoreCase(options[index + 1])) {
options[index] = options[index] + ": " + "None";
index++;
} else {
String[] c = textFile.get(i).split(" ");
if (!formattedPID[index].contains(c[0])) {
optionsOutOfBoundsIndex = formattedPID[index] + " " + options[index];
outofBoundsIndex.add(optionsOutOfBoundsIndex);
for (int a = 0; a < c[0].length(); a++) {
optionsOutOfBounds = optionsOutOfBounds + " " + c[1];
outOfBounds[index] = optionsOutOfBounds;
}
}
if (formattedPID[index].equals(c[0])) {
if (!options[index].equals(options[8])) {
options[index] = options[index] + ": " + c[1];
index++;
obj = false;
} else if (options[index].equals(options[8])) {
options[index] = options[index] + ": ";
StringJoiner joiner = new StringJoiner(", ");
for (int j = 0; j < c.length; j++) {
if (j % 2 != 0 && j != 0) {
joiner.add(c[j]);
obj = false;
}
}
options[index] = options[index] + joiner;
}
}
}
}
}
if (obj) {
options[index] = options[index] + ": None";
}
//Second Attempt to Catch Model Error
try {
if (options[0].contains("None")) {
throw new ModelNotFoundException(formattedPID[0], getMake());
}
} catch (ModelNotFoundException e) {
System.out.println(e);
AnyCarManufacturer.request();
}
//This trims down my arraylist of out of bound options
List<String> list = new ArrayList<String>();
for (String text : outOfBounds)
{
if (text != null && text.length() > 0)
{
list.add(text);
break;
}
}
outOfBounds = list.toArray(new String[0]);
String[] splitter = new String[2];
splitter = outofBoundsIndex.get(0).split(" ");
System.out.println(splitter[0]+" "+splitter[1]);
//Attempt to Catch Options Error
if (outOfBounds[0]!=null){
try {
throw new OptionsNotFoundException(outOfBounds[0]);
} catch (OptionsNotFoundException e) {
System.out.println(e);
AnyCarManufacturer.request();
}
}
}