Friday, 4 October 2013

DM FINDING MISSING VALUE CODE IN JAVA

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Finder {

/**
* @param args
*/
public static void main(String[] args) {
String csvFile = "E:/Documents and Settings/student/Desktop/sample data 1.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
String[] reader;
File file = new File("E:/Documents and Settings/student/Desktop/sample data without nulls.csv");
FileWriter fw = null;
int count=0;
try {
fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
        // use comma as separator
reader = line.split(cvsSplitBy);
//System.out.println("Age " + country[1]  + "Marital Status" + country[3]);
for(int i=0;i<5;i++){
if(reader[i].length()==0) {
reader[i]="missing";
count++;
}
}
bw.write(reader[0]+","+reader[1]+","+reader[2]+","+reader[3]+","+reader[4]+"\n");
}
bw.close();
System.out.println("Number of missing values="+count+"Done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

 finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 }
}
}


No comments:

Post a Comment