Skip to content

Instantly share code, notes, and snippets.

@hugo4715
Created December 19, 2019 10:33
Show Gist options
  • Save hugo4715/81ffc6bff8f9a5e42861296bffd130eb to your computer and use it in GitHub Desktop.
Save hugo4715/81ffc6bff8f9a5e42861296bffd130eb to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static final int MAX_PER_LINE = 6;
public static final String FILE_IN = "C:\\Users\\denia\\Desktop\\val temps int.csv";
public static final String FILE_OUT = "C:\\Users\\denia\\Desktop\\val temps int - Copie.csv";
public static void main(String[] args) throws IOException {
System.out.println("Loading dataset");
List<String[]> dataset = loadDataset(FILE_IN);
System.out.printf("Loaded %s rows", dataset.size());
System.out.println("Processing rows...");
Iterator<String[]> iterator = dataset.iterator();
String[] last = iterator.next();
int max = last.length;
while(iterator.hasNext()){
String[] current = iterator.next();
//if time stamps are equal
if(last[0].equals(current[0])){
System.out.printf("Found duplicate '%s'\n", last[0]);
iterator.remove();//remove line
//find free index in last values and replace it
for (int i = 2; i < last.length; i++) {
if(last[i] == null){
last[i] = current[1];//replace by new value
break;
}
}
}else{
last = current;
max = Math.max(max, current.length);
}
}
//replace null
System.out.println("Replacing empty cells...");
int finalMax = max;
dataset = dataset.stream().map(values -> {
String[] newValues = new String[finalMax];
for (int i = 0; i < finalMax; i++) {
if(values[i] != null){
newValues[i] = values[i];
}else{
newValues[i] = "S0";
}
}
return newValues;
}).collect(Collectors.toList());
System.out.println("Saving result");
writeDataset(dataset, FILE_OUT);
System.out.println("Saved");
}
private static void writeDataset(List<String[]> dataset, String file) throws IOException {
try(FileWriter fw = new FileWriter(file); BufferedWriter br = new BufferedWriter(fw)){
for (String[] strings : dataset) {
for (int i = 0; i < strings.length; i++) {
if(i != 0)br.append(',');
br.append(strings[i]);
}
br.append('\n');
}
}
}
private static List<String[]> loadDataset(String file) throws IOException {
try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {
List<String[]> v = new ArrayList<>();
String line;
//load csv
while ((line = br.readLine()) != null) {
v.add(line.split(";"));
}
//extends arrays
v = v.stream().map(values -> {
String[] newValues = new String[MAX_PER_LINE];
System.arraycopy(values,0,newValues, 0, values.length);
return newValues;
}).collect(Collectors.toList());
return v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment