How to read and write a CSV file using core java?


Hello Buddies.

CSV: CSV is a comma separated values file which allows data to be saved in a table structured format


Let's try to read a CSV file using java, filter out the required rows and create a new CSV per our own requirements.


Input - Provided as a path for the file having below details -


ID NAME AGE GENDER OCCUPATION ADDRESS
1 ABC 22 M SE IN
2 DEF 32 F SE US
3 ABC 12 M SE UK
4 DEF 42 F SE IN
5 ABC 52 M SE US
6 DEF 53 F SE UK
7 ABC 60 M SE IN
8 DEF 67 F SE US
9 ABC 74 M SE UK
10 DEF 81 F SE IN
11 ABC 88 M SE US








We will filter out the people living in India and will have a new CSV only for those people.

Here's a code snippet -

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

public class CSVReader {

 private static String mainPath = "C:\\\\Users\\\\namit.sharma\\\\Downloads\\\\";

 public static void main(String[] args) throws IOException {

  FileReader fileReader = new FileReader(new File(mainPath + "sampleExcel1.csv"));

  BufferedReader bufferedReader = new BufferedReader(fileReader);

  String string;
  do {
   string = bufferedReader.readLine();
   if (null != string) {
    System.out.println(string);
    filterForIndians(string);
   }
  } while (null != string);

  bufferedReader.close();

 }

 private static void filterForIndians(String string) throws IOException {
  File file = new File(mainPath + "fileredCsv.csv");
  if (file.exists()) {
   file.delete();
  }
  FileWriter fileWriter = new FileWriter(new File(mainPath + "fileredCsv.csv"),true);
  if ("IN".equals(string.split(",")[5])) {
   BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
   bufferedWriter.write(string+"\n");
   bufferedWriter.close();
  }
 }

}


Now we can check the generated csv. It will have the desired rows -

1 ABC 22 M SE IN
4 DEF 42 F SE IN
7 ABC 60 M SE IN
10 DEF 81 F SE IN

How to count the number of leaves in a binary tree?


Hello Buddies.


Here's a quick code snippet for finding the number of leaves in a 'Binary Tree', after a node gets deleted!





Have a look-

Input -

5
-1 0 0 1 1
1


Input description -

Line 1: No. of nodes in Binary Tree
Line 2: All the nodes' parents, where -1 means no parent - root, 0 means root as parent.
Line 3: Node at which this value exists, remove it.


Node to remove -



public class TreeProblem {
 private static int count = 0;

 public static void main(String args[]) throws Exception {

  Scanner scanner = new Scanner(System.in);
  int numOfNodes = scanner.nextInt();
  scanner.nextLine();
  String intStr = scanner.nextLine();
  String[] array = intStr.split(" ");
  int[] arr = new int[array.length];
  for (int i = 0; i < numOfNodes; i++) {
   arr[i] = Integer.valueOf(array[i]);
  }
  int nodeToDelete = scanner.nextInt();

  // create a tree
  Node root = null;
  int valueIndex = 0;
  Node current = null;
  for (int i = 0; i < numOfNodes; i++) {
   if (arr[i] == -1) {
    root = new Node(valueIndex++);
    current = root;
   } else {
    if (arr[i] == current.value) {
     if (null == current.left) {
      current.left = new Node(valueIndex++);
     } else if (null == current.right) {
      current.right = new Node(valueIndex++);
      current = current.left;
     }
    }
   }
  }

  // scan the tree for deleting the node
  // it clearly means we have to ignore the leaves at that node, for the remaining
  // keep the count going

  treetraversal(root, nodeToDelete);

  System.out.println("Remaining leaves = " + count);
  scanner.close();
 }

 private static void treetraversal(Node root, int nodeToDelete) {
  if (null == root) {
   return;
  }
  System.out.println("-- " + root.value);
  if (root.value == nodeToDelete) {
   return;
  }
  if (null != root.left) {
   treetraversal(root.left, nodeToDelete);
  }
  if (null != root.right) {
   treetraversal(root.right, nodeToDelete);
  }
  if (null == root.left && null == root.right) {
   count++;
  }
 }

}

class Node {

 int value;
 Node parent, left, right;

 Node(int value) {
  this.value = value;
 }
}

Featured post

Oracle SQL Scheduled Jobs - An Interesting Approach

  Oracle SQL Scheduled Jobs A DB Scheduler is the best way to automate any backend database job. For instance, if you want to process the p...