Inter-row Calculations

Question

I am working with a CSV file and 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 values 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 “no such element” exception for my code.

 

Answer

This is a simple inter-row calculation. For an odd number row, column c value is (a2*next row’s c value)+(a1*current c value); for an even number row, column c value is (a3*previous row’s c value)+(a4*current c value).

Hardcoding in Java involves a lot of detail-handling, which is error-prone. A better alternative is SPL. The Structured Process Language generates simple and easy code and the script is integration-friendly.

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: Read the csv file.

A2: Assign values to four cellset variables – a1, a2, a3, a4.

A3: Run a loop to modify column c values for each row. c=a2*c[1]+a1*c for an odd number row, and c=a3*c[-1]+a4*c for an even number row. # represents a row number, and c[-1] represents  the previous c value.

A4: Export the modified data to a target csv file.

See How to Call an SPL Script in Java to learn more about calling an SPL script in Java.