将多个值读入数组错误

时间:2014-10-10 15:05:50

标签: java arrays file loops

所以我必须设计一个读取股票代码,名称,以前的收盘价和当前价格的类。然后我让这个驱动程序实例化不同的数组,用它做不同的事情。我无法将数据作为文件读取。该文件有字符串和数字,所以我决定将所有数据作为字符串读取,然后将我需要的数据解析为双精度数。我相信当我从文件中读取时会发生错误。我收到了这个错误:

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 个答案:

答案 0 :(得分:1)

Tesla Motors缺少一个价格线:

TSLA
Tesla Motors
259.28
AAPL

因此,您尝试将AAPL转换为double

如果这是正常的,您应该在阅读价格之前使用hasNextDouble()方法。