I want to modify on column values of csv file with large dataset. so I had extracted on single column values(here 2nd) then find standard deviation by 2 time iteration of while loop.1st for find mean and 2nd for find standard deviation. standard deviation is multiply with extracted value and that values are replace with it. then generate updated csv file. here when I run code it generate new file successfully with blank file by without while loop iteration. i think there is something problem with both while loop or it is not reading a file. i don't know what it is? standard deviation(σ = √[(Σ(x - MEAN))2 ÷ n]) pls help me
package csvtest7;
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.*;
public class Csvtest7 {
public static void main(String[] args)throws IOException {
String filename = "ly.csv";
File file = new File(filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("ly_updated.csv"));
}
catch (IOException e) {
}
try {
Scanner inputStream = new Scanner(file);
inputStream.next();
double Tuple;
int count=0;
Tuple = 0;
double stddev=0;
double stddev1;
double stddev2;
//double Xi;
double MEAN;
double standarddeviation;
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(";");
double balance = Double.parseDouble(values[2]);
balance = balance + 1;
Tuple += balance ;
}
MEAN=Tuple/count;
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(";");
double balance = Double.parseDouble(values[2]);
stddev=balance-MEAN;
stddev1=(stddev*stddev);
stddev2=(stddev1/count);
standarddeviation=Math.sqrt(stddev2);
balance=standarddeviation*balance;
values[2] = String.valueOf(balance);
// iterate through the values and build a string out of them
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(";");
}
}
// get the new string
System.out.println(sb.toString());
writer.write(sb.toString()+"\n");
}
writer.close();
inputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Csvtest7.class.getName()).log(Level.SEVERE, null, ex);
}
}
You are skipping the second while loop.
You are executing first while loop while (inputStream.hasNext()) {
successfully until there are no more tokens to read from the file. Now your second while loop again says while (inputStream.hasNext()) {
Now since you already read the file, it wont move the pointer back to start of the file and it would say that there are no more tokens to read from the file and hence skips the second while loop.
One way to resolve this issue is to redefine the inputStream as:
inputStream = new Scanner(file);
while (inputStream.hasNext()) {//start second while loop.
Or
else within your first while loop, you could do processing what you are trying to do in second while loop. You don't need second while loop.