0

So I had to design a class that read in stock symbol,name,previous closing price and current price. I then made this driver to instantiate different arrays to do different things with it. I had trouble reading the data as a file. The file has strings and numbers so I decided to read all the data as strings and then parse the ones I needed into doubles. I believe the error occurs when I am reading from the file. I am getting this error:

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)

Any help would be appreciated. Here is my StockDriver code:

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

Here is my stock class code:

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

And here is my text that I am reading in:

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
2
  • 1
    Read exception again.. (tip: it says that the line you are reading cannot be parsed as a number) Commented Oct 10, 2014 at 15:07
  • You're not reading the line you think you're reading; the obvious debugging step is to dump the values of your strings before parsing them to be sure they're what you expect. Commented Oct 10, 2014 at 15:09

1 Answer 1

1

Tesla Motors is missing one price line:

TSLA
Tesla Motors
259.28
AAPL

Therefore you try to convert AAPL to double.

If this is normal, you should use hasNextDouble() method before reading the price.

Sign up to request clarification or add additional context in comments.

1 Comment

Why would we? If it helped you, please accept the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.