2017-03-25 17 views
-1
/** 
* 5 points 
* 
* Return the price of the stock at the given date as a double. Each line 
* of the file contains 3 comma separated 
* values "date,price,volume" in the format "2016-03-23,106.129997,25703500" 
* where the data is YYYY-MM-DD, the price 
* is given in USD and the volume is the number of shares traded throughout 
* the day. 
* 
* Note: You don't have to interpret dates for this assignment and you can use 
* the Sting's .equals method to 
* compare dates whenever date comparisons are needed. 
* 
* @param stockFileName The filename containing the prices for a stock for each 
*      day in 2016 
* @param date   The date to lookup given in YYYY-MM-DD format 
* @return The price of the stock represented in stockFileName on the given date 
*/ 
public static double getPrice(String stockFileName, String date) { 
    try { 
    BufferedReader br = new BufferedReader(new FileReader(stockFileName)); 
    String line = ""; 
    String unparsedFile = ""; 
    Double Price; 
    while ((line = br.readLine()) != null) { 
     String[] Ans = unparsedFile.split(","); 
     for (String item : Ans){ 
     if(Ans[1].equals(date)){ 
      double aDouble = Double.parseDouble(Ans[2]);  
      return aDouble; 
     } 
     } 
    } 
    br.close(); 

    } catch (IOException ex) { 
    System.out.println("Error"); 
    } 
    return Ans; 
} 

ここでは、.csvファイルの1つの列と日付パラメータを比較することを前提としています。コードで個々の行を探し、その行の[1]と日付パラメータを比較し、その行の[2]を返します。2つの.csvファイルを比較するには?

+0

  • つのスペースは、私たちは言葉でより多くの説明を取得することはできますか? – surajsn

  • 答えて

    0

    あなたのコードは(機能的に)正確に近いと思います。あなたが作った間違いは、Java配列はゼロからではなくインデックスから取得されるということです。だから、Ans[1]は実際には配列の2番目の要素を与えています...最初の要素ではありません。

    解決策:明らかに...私が上記で書いたことを理解していると仮定します。あなたはバグ(複数可)を固定したら

    することは、あなたはスタイルの問題を修正する必要があります。

    • 必ず小文字で変数の名前を開始します。
    • インデントレベルとして4つのスペースを使用します。
    • ifの後に1つのスペース。 ){
    関連する問題