首页 > 解决方案 > 如何标记 arrayList 元素,映射到对象并在 Java 中创建对象 ArrayList

问题描述

我是一名正在学习 Java 课程的学生,我正在从事一项作业,该作业涉及将 csv 文件读入 ArrayList,然后使用逗号标记每一行并将每个标记映射到 StockCompanyShare 对象的变量。我已将文件读入 ArrayList 并尝试对其进行标记并解析股票的最高价、最低价、开盘价、收盘价并调整为接近双倍,但是当我尝试编译它时出现此错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Open"
    *at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:549)
    at readFile.StockObject(readFile.java:85)
    at readFile.main(readFile.java:43)`import java.io.File;

任何帮助将不胜感激!

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class readFile {

    public static void main(String[] args) {

      
        //Creates ford file object and passes list of Ford stocks as the value
        File ford = new File ("Ford historical stock price.csv");   
        
        //try and catch block
        try {
            
            //Creates Scanner object fscan and sets it scan ford
             Scanner fscan = new Scanner(ford);
             
            //Read the file into an arraylist of string values.
             
             //creates ArrayList of strings called stocks
                ArrayList<String> stocks = new ArrayList<>();
                
                //while there is another line to scan
                while (fscan.hasNext())
                {
                    //declares String stock and assigns it to next line of fscan
                String  stock = fscan.nextLine();
                    
                //adds stock to the stocks ArrayList
                    stocks.add(stock);
                    
                    //prints stock to the console
                     System.out.println(stock);

                        //prints a blank line between each line of text
                        System.out.println();
                        
                    
                }
        //calls StockObjects method and passes stocks   
        StockObject(stocks);
          }
        
        //catch block that deals with FileNotFoundException 
        catch (FileNotFoundException e) {
                
                //prints the stack trace of methods 
                e.printStackTrace();
            }
        
        
    }

    //Now take the first record of the arraylist and split it and map into a stock. BANG! you got one day.
public static void StockObject( ArrayList<String> stocks) {
      ArrayList <StockCompanyShare> fordStocks = new ArrayList <StockCompanyShare>();

    int nT = 0;
     for(int index = 0;index < stocks.size();index++)
     {
        String stockString = (stocks.get(index)); 
        
        
        //declares string dataTokens
      //   String dataTokens[] = new String[nT];
        
    //aDataRow is equal to the current line
//  aDataRow = fileInput.nextLine();

    //prints the line currently equal to aDataRow
    //System.out.println(aDataRow);

  //splits aDataRow into tokens using commas and names these tokens datsTokens
String[] dataTokens = stockString.split (",");

            
           // StockCompanyShare.setDate(dataTokens[0]);
              String date = (dataTokens[0]);
              
             
        //    StockCompanyShare.setOpen(Double.parseDouble(dataTokens[1]));
              
            double open = Double.parseDouble(dataTokens[1]);
              
            
//          StockCompanyShare.setHigh(Double.parseDouble(dataTokens[2]));
              
            double high = (Double.parseDouble(dataTokens[2]));
            
        // StockCompanyShare.setLow(Double.parseDouble(dataTokens[3]));
              
            
         double low = Double.parseDouble(dataTokens[3]);
              
//          StockCompanyShare.setClose(Double.parseDouble(dataTokens[4]));
              
            double close = ( Double.parseDouble(dataTokens[4]));
              //calls setState method and passes the value of dataTokens [5] as an argument
       // StockCompanyShare.setAdjClose(Double.parseDouble(dataTokens[5]));
            double   adj = (Double.parseDouble(dataTokens[5]));
     
//     StockCompanyShare.setAdjClose(adj);
         //  String adj = ((dataTokens[5]));
         
    //     StockCompanyShare.setVolume((dataTokens[6]));
            
            double vol = (Double.parseDouble(dataTokens[6]));
             

     

            StockCompanyShare s = new StockCompanyShare(date, open, high, low, close, adj, vol);
         
            
           // StockCompanyShare s = new StockCompanyShare(dataTokens[0],dataTokens[1], dataTokens[2], dataTokens[3], dataTokens[4], dataTokens[5], 
            //      dataTokens[6]);
            
            fordStocks.add(s);
           System.out.println("Stock #" + index + " " + s);
            
}
     System.out.println("Object Array:");
     System.out.println(fordStocks);
    } 

    
}
`

//Stock Company Share Object

    import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

/**
 * StockCompanyShare
 * Description
 * Author Jaclyn Brassell
 *Date March 2021
 */
public class StockCompanyShare implements Comparable <StockCompanyShare >{
//declares instance fields low, high, open, close, volume
    String file;
    
    double low;
    
    double high;
    
    double open;
    
    double close;
    
    double volume;
    
    String date;
    double AdjClose;
    
public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public double getAdjClose() {
        return AdjClose;
    }
    public void setAdjClose(double adjClose) {
        AdjClose = adjClose;
    }
/**
 * getLow
 * @return low
 */
    public double getLow() {
        return low;
    }
/**
 * setLow
 * @param low
 */
    public void setLow(double low) {
        this.low = low;
    }
/**
 * getHigh
 * @return high
 */
    public double getHigh() {
        return high;
    }
    
/**
 * setHigh
 * @param high
 */
    public void setHigh(double high) {
        this.high = high;
    }
/**
 * getOpen
 * @return open
 */
    public double getOpen() {
        return open;
    }
/**
 * setOpen
 * @param d
 */
    public void setOpen(double d) {
        this.open = d;
    }
/**
 * getClose
 * @return close
 */
    public double getClose() {
        return close;
    }
/**
 * setClose
 * @param close
 */
    public void setClose(double close) {
        this.close = close;
    }
/**
 * getVolume
 * @return volume
 */
    public double getVolume() {
        return volume;
    }
/**
 * setVolume
 * @param volume
 */
    public  void setVolume(double volume) {
        
        
        this.volume = volume;
    }
public StockCompanyShare(String date, double open,double high, double low, double close, double adjClose, double volume) {
    
    
    this.low = low;
    this.high = high;
    this.open = open;
    this.close = close;
    this.volume = volume;
    this.date = date;
    AdjClose = adjClose;
}
public StockCompanyShare(String d, String open2, double high2, double low2, double close2, double adj, double volume2) {
    // TODO Auto-generated constructor stub
}

@Override
public int compareTo(StockCompanyShare o) {
    StockCompanyShare other = (StockCompanyShare) o;

    if(getOpen()  > other.getOpen())
{
    return 1;
}
else if(getOpen() < other.getOpen())
{
    return -1;
}
else
    return 0;
    
}
}

以下是文件中的几行:

日期 开盘 高 低 收盘 调整 收盘量

1972 年 6 月 1 日 0 2.173495 2.149165 2.15322 0.277406 1091238

1972 年 6 月 2 日 2.15322 2.173495 2.141055 2.149165 0.276884 1174468

1972 年 6 月 5 日 2.149165 2.16944 2.141055 2.149165 0.276884 5209582

1972 年 6 月 6 日 2.149165 2.157275 2.116725 2.124835 0.27375 1424158

标签: javaarraysparsingarraylisttokenize

解决方案


推荐阅读