【发布时间】:2017-11-04 03:43:45
【问题描述】:
我是 java 的初学者,有点陷入这两个问题,所以我正在尝试 让程序逐行读取 CSV 文件。
所以在文件中我有第一行作为字符串,列是双倍的。 所以问题是当它读取第一行时,它读取的标题是双倍的,它给了我一个错误。
顺便说一下是CSV文件
我得到的错误如下 线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“CLOSE”这是第一个错误
第二个错误>>在 sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222) –
第三个错误>>在 java.lang.Double.parseDouble(Double.java:510)
第四个错误 >>> 在 AlgorithmTrader.ReadInputData(AlgorithmTrader.java:63)
第五个错误>>在 AlgorithmTrader.Run(AlgorithmTrader.java:16)
最后一个错误 >> SimpleAlgorithmTradingPlatform.main(SimpleAlgorithmTradingPlatform.java:15)
所以文件的第一行有 TIMESTAMP |关闭 |高 |低 |开放|卷,在每一行下面都有双倍的数字,除了卷有整数
您的建议将不胜感激。谢谢
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class AlgorithmTrader {
public void Run() {
ReadInputData();
}
public void ReadInputData() {
// create object of scanner class for user input
Scanner scan = new Scanner(System.in);
// declare file name for input file
String inputFileName = "";
// input from user for input file
System.out.print("Enter Input File Name: ");
inputFileName = scan.nextLine();
try {
PrintWriter pw = new PrintWriter("output.csv");// to open the file
// create a new file
File file = new File(inputFileName);
// create a new scanner object to read file
Scanner readFile = new Scanner(file);
// for each line data
String line = "";
line = readFile.nextLine();//skip the first line
while (readFile.hasNextLine()) {
readFile.nextLine();
// pass file to scanner again
readFile = new Scanner(file);
ArrayList<String> list = new ArrayList<String>();
// read stock data line by line
while (readFile.hasNextLine()) {
// read line from file
line = readFile.nextLine();
// split line data into tokens
String result[] = line.split(",");
// variables to create a Stock object
String timestamp = result[0];
double close = Double.parseDouble(result[1]);
double high = Double.parseDouble(result[2]);
double low = Double.parseDouble(result[3]);
double open = Double.parseDouble(result[4]);
int volume = Integer.parseInt(result[5]);
// store data into ArrayList
list.add(readFile.next());
pw.print(list.add(readFile.next()));
Stock stock = new Stock(timestamp, close, high, low, open, volume);
}// end of while to read file
//close readFile object
readFile.close();
pw.close();//close file
}
} catch (FileNotFoundException e1) {
System.out.println(" not found.\n");
System.exit(0);
} catch (IOException e2) {
System.out.println("File can't be read\n");
}
}
}
我有另一个文件股票类
public class Stock {
String timestamp;
double close;
double high;
double low;
double open;
int volume;
Stock(String t, double c, double h, double l, double o, int v) {
timestamp = t;
close = c;
high = h;
low = l;
open = o;
volume = v;
}
public void settimestamp(String t) {
this.timestamp = t;
}
public void setclose(double c) {
this.close = c;
}
public void sethigh(double h) {
this.high = h;
}
public void setopen(double o) {
this.open = o;
}
public void setvolume(int v) {
this.volume = v;
}
public String gettimestamp() {
return timestamp;
}
public double close() {
return close;
}
public double high() {
return high;
}
public int volume() {
return volume;
}
}
另外一个文件中的main方法也是如此
import java.text.DecimalFormat;
public class SimpleAlgorithmTradingPlatform {
public static void main(String[] args) {
DecimalFormat fmt = new DecimalFormat("#0.00"); // to get the DecimalFormat
AlgorithmTrader test = new AlgorithmTrader();
test.Run();
}
}
【问题讨论】:
-
请将所有相关信息添加到问题中。不像 cmets
-
我已经编辑过,希望你能理解我想要做什么谢谢
-
检查代码中的这一行:AlgorithmTrader.ReadInputData(AlgorithmTrader.java:63) 第 63 行。
-
就是这一行 / double close = Double.parseDouble(result[1]);
-
我已经知道线路有问题但我不知道如何解决它!