2016-11-29 3 views
0

私は、継承を扱うプログラムを開発中です。以下は私のスーパークラスバスから拡張されたクラスの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 
+0

を使用すると、入力文字列の場合は '//エラーによって何を意味するのか:「バハマ」' –

+2

ちょうどループ条件を変更してください。 'j Titus

+0

恐ろしいWombatは、私が格納しようとしている配列がintであるため、それは私に捨てるエラーです。これは、ループの設定によるものです。 @Titusあなたはより詳細な答えを下に書くことができますか? – Touchpad

答えて

1

は、私はあなたがforループのバグを持っていると思います。値を一度だけ読みたいときは、内部ループを複数回実行しています。次のようにあなたのロジックを書き換えることができ -

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")){ 

    if(str.length>7){ 
      for(int i = 0; i < 3; i++){ 
      fare[i] = Integer.parseInt(str[i+4]); 
      } 
     } 

     if(str.length>10){ 
     for(int i = 0; i < 3; i++){ 
      numOfPassenger[i] = Integer.parseInt(str[i+7]); 
      } 
     } 

     busInformation.add(new BusTrip(str[1], 
          Integer.parseInt(str[2]), 
          Integer.parseInt(str[3]), 
          fare, 
          numOfPassenger, 
          str[10])); 

    }//end if 
    }//end while  
}//end readAllBus 
+0

を参照してください。 2つの配列があることが分かってから、2回forループする必要があると思いました。しかし、それは私の最初のループの中にあったので、私の内面だけがローカルスケールを考慮していないのですか? – Touchpad

関連する問題