2014-10-10 57 views
0

因此,我必須設計一個類,讀取股票代碼,名稱,以前的收盤價和當前價格。然後,我讓這個驅動程序實例化不同的數組,用它做不同的事情。我無法將數據作爲文件讀取。該文件包含字符串和數字,因此我決定將所有數據作爲字符串讀取,然後將我需要的數據解析爲雙精度數據。我相信當我從文件中讀取時會發生錯誤。我得到這個錯誤:將多個值讀入數組錯誤

Exception in thread "main" java.lang.NumberFormatException: For input string: "AAPL" 
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) 
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) 
at java.lang.Double.parseDouble(Double.java:538) 
at StockDriver.setStockData(StockDriver.java:31) 
at StockDriver.main(StockDriver.java:10) 

任何幫助,將不勝感激。這裏是我的StockDriver代碼:

import java.text.DecimalFormat; 
import java.io.*; 
import java.util.Scanner; 
import java.io.IOException; 

public class StockDriver { 
    public static void main(String [] args) throws IOException{ 
    Stock [] myStock = new Stock[10]; 

    setStockData(myStock); 
    displayStockData(myStock); 
    } //end main 

    public static void setStockData(Stock [] myStock) throws IOException { 
     File infile = new File("stockData.txt"); 
     if(!infile.exists()){ 
     System.out.println("No file"); 
     System.exit(0); 
     } 
     Scanner scan = new Scanner(infile); 
     String symbol, name, previousClosingPriceString, currentPriceString; 
     double previousClosingPrice, currentPrice; 

     int i = 0; 
     while(scan.hasNext() && i < myStock.length){ 
     symbol = scan.nextLine(); 
     name = scan.nextLine(); 
     previousClosingPriceString = scan.nextLine(); 
     previousClosingPrice = Double.parseDouble(previousClosingPriceString); 
     currentPriceString = scan.nextLine(); 
     currentPrice = Double.parseDouble(currentPriceString); 
     myStock[i] = new Stock(symbol, name, previousClosingPrice, currentPrice); 
     i++; 
     } //end while 
    } //end setStockData 

    public static void displayStockData(Stock [] myStock) { 
    DecimalFormat formatter = new DecimalFormat("#.00"); 
     for(int i = 0; i < myStock.length; i++){ 
     System.out.println(myStock[i]); 
     System.out.println("---------------"); 
     }//end for 
    } //end displayStockData 

} //end class 

這裏是我的股票類代碼:

import java.text.DecimalFormat; 
public class Stock{ 
    private String symbol; 
    private String name; 
    private double previousClosingPrice; 
    private double currentPrice; 

    public Stock(){ 
     symbol = ""; 
     name = ""; 
     previousClosingPrice = 0.0; 
     currentPrice = 0.0; 
    }//end default constructor 

    public Stock(String symbol, String name, double previousClosingPrice, double currentPrice){ 
     this.symbol = symbol; 
     this.name = name; 
     this.previousClosingPrice = previousClosingPrice; 
     this.currentPrice = currentPrice; 
    }//end overloaded constructor 

    public void setSymbol(String symbol){ 
     this.symbol = symbol; 
    } 

    public void setName(String name){ 
     this.name = name; 
    } 

    public void setPreviousClosingPrice(double previousClosingPrice){ 
     this.previousClosingPrice = previousClosingPrice; 
    } 

    public void setCurrentPrice(double currentPrice){ 
     this.currentPrice = currentPrice; 
    } 

    public String getSymbol(){ 
     return symbol; 
    } 

    public String getName(){ 
     return name; 
    } 

    public double getPreviousClosingPrice(){ 
     return previousClosingPrice; 
    } 

    public double getCurrentPrice(){ 
     return currentPrice; 
    } 
    public void getChangePercent(double percentage){ 
     double changePercent; 
     changePercent = previousClosingPrice - (currentPrice/100); 
    } //end getChangePercent() 

    public boolean equals(Stock anyStock){ 
     if (this.name.equals(anyStock.getName()) && this.currentPrice == anyStock.getCurrentPrice() && 
      this.symbol.equals(anyStock.getSymbol()) && 
      this.previousClosingPrice == anyStock.getPreviousClosingPrice()&& 
      this.currentPrice == anyStock.getCurrentPrice()) 

     return true; 
     else 
     return false; 
    } //end equals() 

    public String toString() { 
     String str = ""; 
     str += "Stock Symbol  : " + symbol; 
     str += "\nStock name  : " + name; 
     str += "\nPrevious Price : " + previousClosingPrice; 
     str += "\nCurrent Price : " + currentPrice; 
     return str; 
    } //end toString 
}//end class 

這裏是我的文字,我在我讀:

GPRO 
GoPro, Inc. 
89.93 
89.8773 
SBUX 
Starbucks 
75.26 
75.76 
JCP 
JC Penney 
8.18 
7.72 
AMZN 
Amazon 
323.71 
319.94 
AE 
Adams Resources and Energy 
44.71 
44.69 
CEP 
Constellation Energy Partners 
3.38 
3.35 
KO 
Coca-Cola 
43.66 
44.44 
MCD 
McDonald's 
92.81 
93.53 
TSLA 
Tesla Motors 
259.28 
AAPL 
Apple Inc 
100.80 
102.30 
+1

再次讀取異常..(提示:它表示您正在閱讀的行不能被解析爲數字) – Serhiy 2014-10-10 15:07:50

+0

您並不正在閱讀您認爲正在閱讀的行;顯然的調試步驟是在解析它們之前轉儲字符串的值,以確保它們符合您的期望。 – mikebabcock 2014-10-10 15:09:09

+0

感謝您的幫助。 – 2014-10-10 15:11:35

回答

1

Tesla Motors缺少一個價line:

TSLA 
Tesla Motors 
259.28 
AAPL 

因此,您嘗試將AAPL轉換爲double

如果這是正常的,您應該在讀取價格之前使用hasNextDouble()方法。

+0

謝謝,你可以刪除這個線程 – 2014-10-10 15:10:10

+0

我們爲什麼?如果它幫助你,請接受答案:) – 2014-10-10 15:11:40