gpt4 book ai didi

java - 在java中读取流文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:39 24 4
gpt4 key购买 nike

我正在处理逗号分隔值文件。我想从每个原始文件中提取第一个位置 [数组中的“0”位置] 值,并希望对其进行一些数学计算。

csv inputfile is like this
a,b,c,d
12,32,45,76
23,45,77,56
32,34,49,28
73,92,26,68
73,36,77,26

为了获得第一名,它给我这样的错误

Exception in thread "main" java.lang.NumberFormatException: For input string: ""12"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1268)

at java.lang.Double.parseDouble(Double.java:548)
at rotation.pkg45.Rotation45.main(Rotation45.java:49)//code line no-49

它在第二个和第三个位置上工作正常,但对于第四个位置它给出与第一个相同的错误。

 package rotation.pkg45;import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class Rotation45 {
public static void main(String[] args)throws IOException {
String filename = "bank-full2.csv";
ArrayList<String> namesList = new ArrayList<String>( );
String[] t1;
// StringBuilder sb;
List<Double> list = new ArrayList<Double>();
File file = new File(filename);
BufferedWriter writer = null;

try {
writer = new BufferedWriter(new FileWriter("bank2test1.csv"));
double a1=0.866025;
double a2=0.5;
double a3=-0.5;
double a4=0.866025;
double b1;
double b2;
double c1;
double c2;

Scanner inputStream = new Scanner(file);
inputStream.next();
Scanner inputStreamm = new Scanner(file);
inputStreamm.next();

while (inputStreamm.hasNext()) { //while loop for find MEAN
String data = inputStreamm.next(); //read each line and store in data
String[] values = data.split(","); //every line splited with " ; " and store each attribute in string list

double first = Double.parseDouble(values[1]);


/* no suchelementexeption */ String data1 = inputStreamm.next(); //read each line and store in data
String[] values1 = data1.split(",");
//inputStream.next();
double second = Double.parseDouble(values1[1]);

c1= ((a2*second)+(a1*first));
c2= ((a3*first)+(a4*second));
values1[2] = String.valueOf(c2);
values[2] = String.valueOf(c1);
StringBuilder sb = new StringBuilder();
// String newData = sb.toString();
for (int i = 0; i<values.length ; i++) {
sb.append(values[i]);
if (i < values.length - 1) {
sb.append(",");
}
}
sb.append("\n");
for (int i = 0; i<values1.length ; i++) {
sb.append(values1[i]);
if (i < values.length - 1) {
sb.append(",");
}
}
// get the new string
// System.out.println(sb.toString());

writer.write(sb.toString()+"\n");
}
writer.close();

inputStreamm.close();


}
catch (FileNotFoundException ex)
{
Logger.getLogger(Rotation45.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

例如,这里我提取了值[1](表示数组中的第二个位置,如 32,45,34,..)

so result will be..
12,34,45,76
23,46,77,56
32,36,49,28
73,93,26,68
73,38,77,26

此代码适用于 values[1] 和 value[2] 而不是 values[0] 和 value[3]..为什么我不明白请帮助我...

最佳答案

当您阅读该行时,它显然会返回周围的引号,例如:

"12,32,45,76"

所以当你拆分它时,你会得到以下元素:

"12
32
45
76"

如您所见,行中的第一个和最后一个元素不是数字,因此您的 Double.ParseDouble(..) 调用失败。

您应该修改原始字符串(通过对其进行子字符串化或更好地使用 .reaplce("\"", ""))或在拆分后检查每个元素然后替换/修剪引号。

关于java - 在java中读取流文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30229633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com