これは私がやろうとしていることです:私はコマンドラインからファイルを読み込んでいます。 ファイルにはデータのリストが含まれていますが、この段落の下にはそのように見えます。 私が抱えている問題はif文です。擬似コードで配列インデックスの範囲外
import java.util.*;
import java.io.*;
public class VehicleTest {
public static void main(String[] args) throws FileNotFoundException {
String vehicle = "vehicle";
String car = "car";
String americanCar = "american car";
String foreignCar = "foreign car";
String truck = "truck";
String bicycle = "bicycle";
File file = new File(args[0]);
Scanner input = new Scanner(file);
String[] autos = new String[100];
ArrayList allVehicles = new ArrayList();
for (int i = 0; i < autos.length; i++) {
autos[i] = input.nextLine();
}
int j = 0;
int i = 0;
while (i++ < autos.length) {
if (vehicle.equalsIgnoreCase(autos[j++])) {
Vehicle v = new Vehicle();
v.setOwnerName(autos[j]);
allVehicles.add(v);
}else if(car.equalsIgnoreCase(autos[j++])){
Car c = new Car();
c.setOwnerName(autos[j]);
allVehicles.add(c);
}
}
for(Object a: allVehicles){
System.out.println(a);
}
}
}
これは次のようになります。
while i is less than the length of the string array
if you see the word vehicle create a new vehicle object and add it to the arrayList.
if you see the word car create a new car object and add it to the arrayList.
.....
問題私は私が使用しているコードでarrayOutOfBounds例外を取得していることです。
私はJ ++が間違っていることを理解していますが、文字列配列を繰り返し処理して各行を読み込んで適切なオブジェクトを作成する方法を教えてください。私は何をすべきかについて迷っている。助けが必要です。
foreign car
aMarioy
Mario's house
(777) 777-7777
[email protected]
false
black
Italy
4415.91
truck
aDougy
Doug's house
(123) 456-7890
[email protected]
30
61234.56
8/10/2003
vehicle
aRobby
Rob's house
(987) 654-3210
[email protected]
bicycle
bTommy
Tom's house
(246) 810-1214
[email protected]
7
truck
bGeorge
George's house
(666) 666-6666
[email protected]
25
51234.56
12/4/2004
vehicle
bTim
Tim's house
(111) 111-1111
[email protected]
bicycle
bJim
Jim's house
(555) 555-5555
[email protected]
5
american car
bJohn
John's house
(888) 888-8888
[email protected]
true
green
false
true
car
cKen
Ken's house
(999) 999-9999
[email protected]
false
orange
foreign car
cMario
Mario's house
(777) 777-7777
[email protected]
false
black
Italy
4415.91
american car
gSam
Sam's house
(333) 333-3333
[email protected]
false
blue
true
false
誰もが説明する前に、「これは代わりにしてみてください」の提案が掲載されているどのように多くの興味深いです実際のe不具合 – Tomalak
@Tomalak、質問には、「私は++が間違っていることを理解していますが、文字列を繰り返し処理して、各行を読み込んで適切なオブジェクトを作成できるようにするにはどうしたらいいですか? – strager
@strager:本当ですが、私はこの行が選択肢の要求であることを意図していたとは思っていません。投稿全体が「なぜこの例外を取得するのですか?」それの上に書かれています。 – Tomalak