我是java的初學者,有點卡在這兩個問題中,所以我想 讓程序逐行讀取CSV文件。當讀取java中的CSV文件時NumberFormatException
所以在文件中我有第一行作爲字符串和列是雙。 所以問題是當它讀取第一行它讀取的標題爲雙重,它給了我一個錯誤。
通過它CSV文件
我得到了錯誤的方式是這些低於 異常線程「main」 java.lang.NumberFormatException:對於輸入字符串:「CLOSE」這是第一個錯誤
第二個錯誤>>在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecima l.java:1222) -
第三個錯誤>>在java.lang.Double.parseDouble(Double.java:510)
福斯錯誤>>>在算法mTrader.ReadInputData(AlgorithmTrader.java:63)
第五錯誤>>在AlgorithmTrader.Run(AlgorithmTrader.java:16)
最後一個錯誤>> SimpleAlgorithmTradingPlatform.main(SimpleAlgorithmTradingPl atform.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");
}
}
}
我還有一個文件股票類
而在另一個文件中的主要方法以及
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();
}
}
請將所有相關信息添加到問題中。沒有評論 – c0der
我已經編輯,希望你明白我想做 感謝 – Abdoh
入住這行代碼:AlgorithmTrader.ReadInputData(AlgorithmTrader.java:63)線63 –