modify and generate new csv file by extracting column values - java

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.

Related

Finishing File Class

I keep getting an error telling me lineNumber cannot be resolved to a variable? I'm not really sure how to fix this exactly. Am I not importing a certain file to java that helps with this?
And also how would I count the number of chars with spaces and without spaces.
Also I need a method to count unique words but I'm not really sure what unique words are.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.List;
public class LineWordChar {
public void main(String[] args) throws IOException {
// Convert our text file to string
String text = new Scanner( new File("way to your file"), "UTF-8" ).useDelimiter("\\A").next();
BufferedReader bf=new BufferedReader(new FileReader("way to your file"));
String lines="";
int linesi=0;
int words=0;
int chars=0;
String s="";
// while next lines are present in file int linesi will add 1
while ((lines=bf.readLine())!=null){
linesi++;}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st=new StringTokenizer(text);
while (st.hasMoreTokens()){
s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;}
}
System.out.println("Number of lines: "+linesi);
System.out.println("Number of words: "+words);
System.out.print("Number of chars: "+chars);
}
}
abstract class WordCount {
/**
* #return HashMap a map containing the Character count, Word count and
* Sentence count
* #throws FileNotFoundException
*
*/
public static void main() throws FileNotFoundException {
lineNumber=2; // as u want
File f = null;
ArrayList<Integer> list=new ArrayList<Integer>();
f = new File("file_stats.txt");
Scanner sc = new Scanner(f);
int totalLines=0;
int totalWords=0;
int totalChars=0;
int totalSentences=0;
while(sc.hasNextLine())
{
totalLines++;
if(totalLines==lineNumber){
String line = sc.nextLine();
totalChars += line.length();
totalWords += new StringTokenizer(line, " ,").countTokens(); //line.split("\\s").length;
totalSentences += line.split("\\.").length;
break;
}
sc.nextLine();
}
list.add(totalChars);
list.add(totalWords);
list.add(totalSentences);
System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences);
}
}
In order to get your code running you have to do at least two changes:
Replace:
lineNumber=2; // as u want
with
int lineNumber=2; // as u want
Also, you need to modify your main method, you can not throw an exception in your main method declaration because there is nothing above it to catch the exception, you have to handle exceptions inside it:
public static void main(String[] args) {
// Convert our text file to string
try {
String text = new Scanner(new File("way to your file"), "UTF-8").useDelimiter("\\A").next();
BufferedReader bf = new BufferedReader(new FileReader("way to your file"));
String lines = "";
int linesi = 0;
int words = 0;
int chars = 0;
String s = "";
// while next lines are present in file int linesi will add 1
while ((lines = bf.readLine()) != null) {
linesi++;
}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st = new StringTokenizer(text);
while (st.hasMoreTokens()) {
s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;
}
}
System.out.println("Number of lines: " + linesi);
System.out.println("Number of words: " + words);
System.out.print("Number of chars: " + chars);
} catch (Exception e) {
e.printStackTrace();
}
}
I've used a global Exception catch, you can separate expetion in several catches, in order to handle them separatedly. It gives me an exception telling me an obvious FileNotFoundException, besides of that your code runs now.
lineNumber variable should be declared with datatype.
int lineNumber=2; // as u want
change the first line in the main method from just lineNumber to int lineNumber = 2 by setting its data type, as it is important to set data type of every variable in Java.

how to reverse arraylist<Double> of extracted column data in file

I am working with csv file having very large dataset. while reading file i had extracted 4th place(BALANCE) ';' separated numeric value from each rows through while loop iteration. and make a arraylist of Double after some mathematical calculation(here division).
Now I want to store this arraylist of Double in reverse order(from end to beginning).as its original position(here 4th place in the file).example
input
1,2,3,4
2,3,4,5
3,4,5,6
output
1,2,3,6
2,3,4,5
3,4,5,4
I had try to reverse it but not succeeded. I don’t know whether it was suitable method for my problem or not. How can I do this?
Then after using string builder I write back data in new file using writer method.
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.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class Csvtest7 {
public static void main(String[] args)throws IOException {
String filename = "sample dataset.csv";
List<Double> list = new ArrayList<Double>();
File file = new File(filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("lyupdated.csv"));
} catch (IOException e) {
}
try {
Scanner inputStream = new Scanner(file);
inputStream.next();
int count = 0;
int number = 11;
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
double balance = Double.parseDouble(values[3]);
balance = balance / number;
count = count+1;
values[3] = String.valueOf(balance);
list.add(balance);
Collections.reverse(list); // I tryied this method but don't work.
// 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);
}
}
}

finding standard deviation in csv file

i am trying to find standard deviation(σ = √[(Σ(x - MEAN))2 ÷ n]) of single extracted column of csv file.csv file contain around 45000 instance and 17 attribute saperated with ';'.
for finding standard deviation it need MEAN value in every iteration of while loop for substact with Xi. so i think MEAN need before while loop iteration for find standard deviation.but i dont know how to do this or is there any way to do this. am getting stuck here. then i had puted code for replace old Xi with new Xi. and then write(generate) new csv file.
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 static java.lang.Math.sqrt;
public class Main {
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("bank-full_updated.csv"));
}
catch (IOException e) {
}
try {
double Tuple,avg;
double temp;
Tuple = 0;
double stddev=0;
Scanner inputStream = new Scanner(file);
inputStream.next();
while (inputStream.hasNext()) {
String data1 = inputStream.next();
String[] values = data1.split(";");
double Xi = Double.parseDouble(values[1]);
//now finding standard deviation
temp1 += (Xi-MEAN);
// temp2=(temp1*temp1);
// temp3=(temp2/count);
// standard deviation=Math.sqrt(temp3);
Xi=standard deviation * Xi
//now replace new Xi to original values1
values[1] = String.valueOf(Xi);
// iterate through the values and build a string out of them for write a new file
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(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
It is possible to calculate the standard deviation in a single pass. Professor Donald Knuth has an algorithm that does it using the Kahan summation. Here is the paper: http://researcher.ibm.com/files/us-ytian/stability.pdf
Here is another way but it suffers from rounding errors:
double std_dev2(double a[], int n) {
if(n == 0)
return 0.0;
double sum = 0;
double sq_sum = 0;
for(int i = 0; i < n; ++i) {
sum += a[i];
sq_sum += a[i] * a[i];
}
double mean = sum / n;
double variance = sq_sum / n - mean * mean;
return sqrt(variance);
}

Reading from a text file into an array

Hi im currently trying to do a hackerearth challenge sum of medians and it involves me reading from a text file and storing the values in an array. The first value has to be stored in a variable N which i am able to do but the the remaining values have to be stored in an array. This is where i become stuck. i have to read each value line by line and then store it in the array .
this is my code that i have been trying to get it working on but i just cant see where im going wrong.
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
// read number of data from system standard input.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
int i = 1;
int[] myIntArray = new int[N];
// median sum
long SumMedians = 0;
int median = 0;
while (i<N)
//read one line file and parse as an integer
//store the value in an array
{
myIntArray [i] = Integer.parseInt(line);
i = i + 1; // increment i so i is the total numbers read
}
so as i said i must increment through the text file storing each value on the line in an array. Any help would be amazing thanks
The text file will look like this
5
10
5
1
2
15
one string per line, which i have to pass into an integer.
what i will be doing is after i store the value from the line into the array i will be sorting it and finding its medium and then repeat this process until all the values from the text file have been read.
The problem which i am trying to do is this one
http://www.hackerearth.com/problem/algorithm/sum-of-medians-1/
If you're reading from a text file (and not from standard input which is what you're doing at the moment) then you want something like:
// Warning: this could fail if the filename is invaild.
BufferedReader br = new BufferedReader(new FileReader("inputFileName.txt"));
To then read in each line, you can use the following in the while loop:
// Warning: this will crash the program if the line contains anything other than integers.
myIntArray[i] = Integer.parseInt(br.readLine())
i = i + 1; // increment i so i is the total numbers read
You should also close the reader at the end:
try{
br.close();
} catch (IOException e)
{
System.out.println("Error, program exit!");
System.exit(1);
}
The import should be swapped from import java.io.InputStreamReader
to: import java.io.FileReader
Since you are only reading 1 line therefore I suspect it to be a single line delimited by colon/semicolon or other character.. try looking into StringTokenizer and Scanner classes
N = the number from parsing a string to a number
In the first part of your program it N = 5
Why are you using while(i<5)?
If anything you should be
r = number of lines in text file;
while (i< r)
{
readline;
parseline;
store in array;
}
and then sort
Adapting the example they gave you
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
//create storage array
int[] myIntArray = new int[N];
//read remainder of file
for (int i = 0; i < N; i++) {
String line = br.readLine();
myIntArray[i] = Integer.parseInt(line);
}
// close file
br.close();
//Perform median calculations
int median = 0;
...
System.out.println(median);
}
}

Importing a .dat File Java

I've searched the internet for roughly an hour and a half now, and I can't for the life of me figure out where I've gone wrong.. Help!!
My problem is that every time I try and run it I don't receive an error until it searches for the file and without fail, it replies "File not found." I'm on a MAC I think I'm typing the directory in properly but something is messed up..
(When opening numEven.dat)
For my input I've tried "numEven.dat" (placing the dat file in the same directory as the java file)
I've also tried "/Users/java/numEven.dat" and "Users/java/numEven.dat"
I know it is in that directory. What am I doing wrong?
Main Class file:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class StatDriver
{
public static void main(String[] args)
{
String fileName = "";
Scanner scan = new Scanner(System.in);
double[] array = new double[20];
System.out.print(" Enter file name: ");
fileName = scan.next();
System.out.println("\n \n \n \n My Grades - View Statistics");
System.out.println(" ------------------------");
// int valueCount = readFile(array,fileName);
array = readFile(array, fileName);
Stat stat = new Stat(array, array.length);
// call each calc on Stat class and display results for each method
stat.calcAvg();
stat.calcMedian();
stat.findMax();
stat.findMin();
// print the return values for each of the above out to the user
}
public static double[] readFile(double[] array, String fileName)
{
int valueCount = 0;
FileIO importFile = new FileIO ();
importFile.main(array, fileName);
System.out.println(array);
valueCount = array.length;
// return valueCount;
return array;
}
}
FileIO class:
import java.util.Scanner;
import java.io.*;
public class FileIO
{
public void main (double[] array, String fileName)
{
double [] num = new double[5];
Scanner inFile;
int i = 0;
try
{
System.out.println(fileName);
inFile = new Scanner(new File("fileName"));
while(inFile.hasNextDouble())
{
array[i] = inFile.nextDouble();
i++;
}
inFile.close();
for(int x = 0; x < i; x++)
System.out.println(" " + num[x]);
}
catch(FileNotFoundException e)
{
System.out.println (" File not found");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (" array index too large");
}
}
}
Try by changing
inFile = new Scanner(new File("fileName"));
with
inFile = new Scanner(new File(fileName));
in the method FileIO.main
Other than that (having no link to the problem), you could make the method FileIO.main static, and take advantage of Java collections to avoid hardcoding the number of elements of the double you want to read from the file. In the same method you are declaring a variable double[] num but not using it at all.

Categories