问题描述
我是一名参加 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,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;
}
}
这是文件中的几行:
日期开盘高低收盘调整收盘量
6/1/1972 0 2.173495 2.149165 2.15322 0.277406 1091238
6/2/1972 2.15322 2.173495 2.141055 2.149165 0.276884 1174468
6/5/1972 2.149165 2.16944 2.141055 2.149165 0.276884 5209582
6/6/1972 2.149165 2.157275 2.116725 2.124835 0.27375 1424158
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)