简体   繁体   English

Java:我如何从文件的每一行中读取4个数字。 并将它们放入变量中?

[英]Java: How do I read 4 numbers from each line in a file. And put them into variables?

The text file looks like this: 文本文件如下所示:

data,data,data,data 数据,数据,数据,数据
data,data,data,data 数据,数据,数据,数据

The data being numbers. 数据为数字。 4 numbers separated by commas and about 2000 lines. 4个数字,以逗号分隔,大约2000行。 However I only need to use 20 lines at a time. 但是我一次只需要使用20行。 The starting line needs to be chosen by its number, and then it gets 19 additional lines. 需要根据其编号选择起始行,然后再获得19行。

This data needs to be put into variables (float) that are named uniquely so I can then do math on them. 该数据需要放入唯一命名的变量(浮点数)中,以便随后对它们进行数学运算。 So it can be named like this: 因此可以这样命名:

DataOne1 DataOne2 DataOne3 DataOne4 DataOne1 DataOne2 DataOne3 DataOne4
DataTwo1 DataTwo2 DataTwo3 DataTwo4 DataTwo1 DataTwo2 DataTwo3 DataTwo4

This way I can then do math like DataTwo1 - DataOne3. 这样,我便可以像DataTwo1-DataOne3一样进行数学运算。 These variables will of course just always be named the same but then I will be able to change the underlying data by picking new lines. 这些变量当然总是被命名为相同的,但是我将能够通过选择新行来更改基础数据。

I'm sorry this is such a beginners question but I've been completely unable to put things together from different solutions to get my exact solution. 抱歉,这是一个初学者的问题,但是我完全无法将不同解决方案的内容放在一起以获得确切的解决方案。

So my question is how do I do this? 所以我的问题是我该怎么做?

1. You can read the entire line using Scanner method nextLine( ). 1.您可以使用Scanner方法nextLine( )读取整行

2. Then use split() method (I am assuming that the data is separated by( "," )commas), to get all the 4 data on the line. 2.然后使用split()方法(我假设数据用(“,”)逗号分隔),以获取该行上的所有4个数据。

eg: 例如:

String[] s = strLine.split(",");

3. Consider making an ArrayList of Float and then convert each item in the String[] array into a Float item in ArrayList , using Float.parseFloat() 3.考虑制造ArrayListFloat ,然后将转换的每个项目中String[]数组成Float在项目ArrayList ,使用 Float.parseFloat()

ArrayList<Float> fArr = new ArrayList<Float>();
for (String temp : s){
    fArr.add(Float.parseFloat(temp));
}

4. Then do whatever calculation you need. 4.然后进行所需的任何计算。

These are the steps you need to do in order to get what you want: 这些是您需要获得所需内容的步骤:

1. Open the file using a BufferedReader : 1.使用BufferedReader打开文件:

BufferedReader br = new BufferedReader(new FileReader(filename));

2. Get to the starting line: 2.到达起点:

for (int i = 0; i < startLine; i++) {
    line = br.readLine();
}

3. Process the 20 lines you need, storing the numbers in an ArrayList <Double> 3.处理所需的20行,将数字存储在ArrayList <Double>

for (int i = startLine; i < startLine + 20; i++) {
   line = br.readLine();

   // Split the lines using comma as delimiter
   String[] numberStrings = line.split(",");
   ArrayList<Double> numbers= new ArrayList<Double>();

   for(String numberString : numberStrings) {
       Double number = Double.valueOf(numberString);
       numbers.add(number);
   }
   // Do calculations with them
}

Here is the code. 这是代码。 For each line, I ve stored the values in array. 对于每一行,我都将值存储在数组中。 So your DataOne1 will be in cols0[0], DataTwo1 will be in cols0[1], DataTwo1 will be in cols1[0] and DataTwo2 will be in cols1[1] and so-on. 因此,您的DataOne1将在cols0 [0]中,DataTwo1将在cols0 [1]中,DataTwo1将在cols1 [0]中,DataTwo2将在cols1 [1]中,依此类推。 You can very well use the two-d array for this purpose, but to keep it simple I ve choosen this approach. 您可以很好地将二维数组用于此目的,但为简单起见,我选择了这种方法。

 try {
        BufferedReader br = new BufferedReader(new FileReader("fileName"));
        String line;
        float[] col0 = new float[20];
        float[] col1 = new float[20];
        float[] col2 = new float[20];
        float[] col3 = new float[20];
        for (int i = 0; i < 20; i++) {
            while((line = br.readLine()) != null) {
                // Since you told comma as separator.
                String[] cols = line.split(","); 
                col0[i] = Float.valueOf(cols[0]);
                col1[i] = Float.valueOf(cols[1]);
                col2[i] = Float.valueOf(cols[2]);
                col3[i] = Float.valueOf(cols[3]);
            }
            // Do something - your math.
        }
        br.close();
    }
    catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM