结构化文本的跨行计算

【问题】

I am working with a csv file, and I want to extract two values in specific positions from each and every row.

The csv input file looks 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

For example I want the two consecutive values from the 3rd position (column c) from every row at the same time, so (45, 77), (49, 26), (77, ???)…

After getting those 2 values, I want to do some calculation on them and store them back.I am working on []2X1 size matrix multiplication. for that reason i need two consecutive value at a time.

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";

        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()) { 

                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[2]);

/*NoSuchElementException*/String data1 = inputStreamm.next(); //read comming nextline for second value and store in data1

                String[] values1 = data1.split(",");

                //inputStream.next();        

                double second = Double.parseDouble(values1[2]);

                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);

    }

}

i am getting error like nosuchelement exception at mention in my code...

【回答】

       简单的行间计算:奇数行将c修改为(a2*下一个行的c)+(a1*本行的c),偶数行则修改为(a3*上一行的c)+(a4*本行的c)

       Java硬编码实现要处理很多细节,代码很多且容易出错,这种情况下可以用SPL实现,脚本很简单,也容易集成。


A

1

=file("d:\\data.csv").import@t(;,",")

2

=a1=0.866025,a2=0.5,a3=-0.5,a4=0.866025

3

=A1.run(c=if(#%2==1,a2*c[1]+a1*c,a3*c[-1]+a4*c))

4

=file("d:\\result.csv").export@t(A1;",")

A1:读取文本

A2a1,a2,a3,a4四个网格变量赋值

A3:循环修改每一行c列的列值

奇数行,则c=a2*c[1]+a1*c,偶数行,则c=a3*c[-1]+a4*c,其中#表示行号,c[-1]表示上一行的c

A4:将修改后的数据导出到文本

写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本