私は、継承を扱うプログラムを開発中です。以下は私のスーパークラスバスから拡張されたクラスの1つです。別のクラスから呼び出されたint配列にファイルからデータをインポートする
public BusTrip(){
super();
int totalPassenger = 0;
fare = new int [3];
numOfPassenger = new int [3];
String destination = "";
}
私のドライバクラスでは、ArrayListにデータをインポートするのに問題があります。私はそれがデータファイルの位置4のすべてを文字列の最後まで読み込んでいることを理解しています。 whileループも試しましたが、それは最初のデータセットを過ぎていないようです。 tokenizerは唯一の方法ですか? forループが特定のポイントを読み取ってしまうのを防ぐ方法がありますか?グレイハウンド
データファイル
CR、2015、30、22、44、14、10、5、15、ニューヨーク
public static void readAllBus(ArrayList<Bus> busInformation){
File infile = null;
Scanner scan = null;
int[] fare = new int [3];
int[] numOfPassenger = new int [3];
try{
infile = new File("busData.csv");
scan = new Scanner(infile);
}//end try
catch(FileNotFoundException e){
System.out.println("Error! File not found!");
System.exit(0);
}//end catch
while(scan.hasNext()){
String [] str = scan.nextLine().split(",");
if(str[0].equals("cr")){
for(int i = 0; i < fare.length; i++)
for(int j = 4; j < str.length; j++)
fare[i] = Integer.parseInt(str[j]);
//error For input string: "Bahamas"
for(int i = 0; i < numOfPassenger.length; i++)
for(int j = 7; j < str.length; j++)
numOfPassenger[i] = Integer.parseInt(str[j]);
//error For input string: "Bahamas"
busInformation.add(new BusTrip(str[1],
Integer.parseInt(str[2]),
Integer.parseInt(str[3]),
fare,
numOfPassenger,
str[10]));
}//end if
}//end while
}//end readAllBus
を使用すると、入力文字列の場合は '//エラーによって何を意味するのか:「バハマ」' –
ちょうどループ条件を変更してください。 'j
Titus
恐ろしいWombatは、私が格納しようとしている配列がintであるため、それは私に捨てるエラーです。これは、ループの設定によるものです。 @Titusあなたはより詳細な答えを下に書くことができますか? – Touchpad