如何在java中仅从文本文件中读取所需的部分数据(double)并将其放入double数组[]
我只想阅读分发点下提到的数字的第一部分。即“-5.34402965372277”,然后将所有这些点存储到java中的数组中如何在java中仅从文本文件中读取所需的部分数据(double)并将其放入double数组[],java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,我只想阅读分发点下提到的数字的第一部分。即“-5.34402965372277”,然后将所有这些点存储到java中的数组中 Using fixed random seed for repeat ability. ............................................ Constructing inference engine of class blog.engine.SamplingEngine Constructing sampler of class blo
Using fixed random seed for repeat ability.
............................................
Constructing inference engine of class blog.engine.SamplingEngine
Constructing sampler of class blog.sample.LWSampler
Evidence: []
Query: [x, y, z]
Running for 20 samples...
Query Reporting interval is 10000
======== LW Trial Stats =========
Log of average likelihood weight (this trial): -4.440892098500626E-16
Average likelihood weight (this trial): 0.9999999999999996
Fraction of consistent worlds (this trial): 1.0
Fraction of consistent worlds (running avg, all trials): 1.0
======== Query Results =========
Number of samples: 20
Distribution of values for x
-5.344029659372277 0.05000000000000003
-3.6282706290477384 0.05000000000000003
-3.497553178865327 0.05000000000000003
-0.8324886197923229 0.05000000000000003
Distribution of values for y
-3.8401834274154405 0.05000000000000003
-2.8617608524112454 0.05000000000000003
-2.8211880095793065 0.05000000000000003
-2.1727081725855695 0.05000000000000003
Distribution of values for z
-3.6803989664398955 0.05000000000000003
-2.3049623281250717 0.05000000000000003
-2.300277789550218 0.05000000000000003
-2.110311642917156 0.05000000000000003
======== Done ========
这是它在博客程序的output.txt中的外观 这里是一个示例,解释是用代码编写的
public static void main(String[] args) throws FileNotFoundException, IOException {
List<String> xValues = new ArrayList<String>();
List<String> yValues = new ArrayList<String>();
List<String> zValues = new ArrayList<String>();
//convert these lists of String to double yourself.
xValues = readValuesFor("x");
yValues = readValuesFor("y");
zValues = readValuesFor("z");
//print contents
System.out.println("X values: ");
for (final String x : xValues) {
System.out.println(x);
}
System.out.println("Y values: ");
for (final String y : yValues) {
System.out.println(y);
}
System.out.println("Z values:");
for (final String z : zValues) {
System.out.println(z);
}
}
public static List<String> readValuesFor(String variableName) throws FileNotFoundException, IOException {
final List<String> result = new ArrayList<String>();
final BufferedReader br = new BufferedReader(new FileReader("output.txt")); //change file name and path to yours
String line;
//read through file until you find a line that end with the variable x ,y, or z
while ((line = br.readLine()) != null) {
if (line.endsWith("for "+variableName.trim())) {
break;
}
}
if (!variableName.equals("z")) { //if variable is x or y
while (!(line = br.readLine()).contains("Distribution")) {
//read the lines between line ending in letters x or y and line containing word "Distribution"
final String[] values = line.split(" ");
result.add(values[0]); //get the first value of the split
}
}
else{
while (!(line = br.readLine()).contains("Done")) { //if variable is z
//read the lines between line ending with letter z and line containing word "Done"
final String[] values = line.split(" ");
result.add(values[0]); //get the first value of the split
}
}
return result; //array with first values as string
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException{
List xValues=new ArrayList();
List yValues=新的ArrayList();
List zValues=new ArrayList();
//将这些字符串列表转换为您自己的两倍。
xValues=读取值,用于(“x”);
y值=读取值(y);
zValues=ReadValues for(“z”);
//印刷内容
System.out.println(“X值:”);
用于(最终字符串x:xvalue){
系统输出println(x);
}
System.out.println(“Y值:”);
for(最终字符串y:y值){
系统输出打印项次(y);
}
System.out.println(“Z值:”);
for(最终字符串z:z值){
系统输出打印ln(z);
}
}
用于(字符串variableName)的公共静态列表readValuesFor引发FileNotFoundException、IOException{
最终列表结果=新建ArrayList();
final BufferedReader br=new BufferedReader(new FileReader(“output.txt”);//将文件名和路径更改为您的文件名和路径
弦线;
//通读该文件,直到找到以变量x、y或z结尾的行
而((line=br.readLine())!=null){
if(line.endsWith(“for”+variableName.trim())){
打破
}
}
如果(!variableName.equals(“z”){//如果变量是x或y
而(!(line=br.readLine())包含(“分发”)){
//阅读以字母x或y结尾的行与包含单词“Distribution”的行之间的行
最终字符串[]值=行。拆分(“”);
result.add(值[0]);//获取拆分的第一个值
}
}
否则{
而(!(line=br.readLine()).contains(“Done”){//如果变量是z
//阅读以字母z结尾的行和包含单词“完成”的行之间的行
最终字符串[]值=行。拆分(“”);
result.add(值[0]);//获取拆分的第一个值
}
}
返回结果;//第一个值为字符串的数组
}
输出为:
X values:
-5.344029659372277
-3.6282706290477384
-3.497553178865327
-0.8324886197923229
Y values:
-3.8401834274154405
-2.8617608524112454
-2.8211880095793065
-2.1727081725855695
Z values:
-3.6803989664398955
-2.3049623281250717
-2.300277789550218
-2.110311642917156
X值:
-5.344029659372277
-3.6282706290477384
-3.497553178865327
-0.8324886197923229
Y值:
-3.8401834274154405
-2.8617608524112454
-2.8211880095793065
-2.1727081725855695
Z值:
-3.6803989664398955
-2.3049623281250717
-2.300277789550218
-2.110311642917156
上述数据在文件中是否有一个特定的起始位置/索引,该位置/索引是恒定的?您是否可以显示您尝试过的内容?从文本文件开始,共有15行文本,然后分发点开始。我正在把一种博客编程语言的输出转换成一个文本文件,并试图将其读入java程序。你能展示一下整个文件和你尝试过的代码吗?那么,到底是什么问题呢?您知道如何使用
扫描仪
,对吗?读前15行,把它们扔掉,然后继续读,把你读到的数字放入一个数组。你试过了吗?分裂不起作用。我得到两个值