Home How to read each line and then find the missing document?
Reply: 2

How to read each line and then find the missing document?

Niraj Nagle
1#
Niraj Nagle Published in 2018-02-14 05:17:34Z

Sample Input format:

Name of the file,  Author,  format type,  id,  content length.
resume,            abc,     pdf,          7,   90
resume,            asc,     doc,          2,   90
resume,            azc,     docx,         3,   90

Output:

Missing document format

pdf

2,3

doc

7,3

Here is my approach: take input from external txt file(Required).

File file = new File("//Users//Downloads//test_cases//input.txt");
ArrayList<String> al=new ArrayList<String>();//creating new generic arraylist  
BufferedReader br = new BufferedReader(new FileReader(file));
String st;  
  while ((st = br.readLine()) != null)                                
    al.add(st);
br.close();     

So, my question is which is apt data structure to use? After reading each line. also how should i approach in storing data ?
A sample code would be great help. thanks in advance.

manji369
2#
manji369 Reply to 2018-02-14 05:40:06Z

You would use hashmaps to store the data with keys as formats and ids as values. If you want to use python, here's a sample:

# Read line from file
with open('filename.txt') as f:
    line = f.readline()
entries = line.split(' ')
# Hashmap to store formats and corresponding ids
formats = {}
# set to store all the ids
ids = set()
for en in entries:
    vals = en.split(',')
    frmt, identifier = vals[2], vals[3]
    # Set of ids for each format
    if frmt not in formats:
        formats[frmt] = set()
    formats[frmt].add(identifier)
    ids.add(identifier)
print("Missing formats")
for frmt in formats:
    print(frmt)
    # Missing formats are set difference between all the ids and the current ids
    print(ids-formats[frmt])
Sree
3#
Sree Reply to 2018-02-17 17:13:28Z

The solution is based on the premise there will be only one value in the entry in the "format type" field.

This solution requires the use of google guava collection. The jars can downloaded from "https://github.com/google/guava/wiki/Release19"

import java.io.BufferedReader;
import java.io.File;
import java.lang.reflect.Array;
import java.util.*;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class FileReader {

public void processData() {

Multimap dataMap = readFile();
dataMap.get("format type");
Object[] array =  ((ArrayListMultimap) dataMap).get("format type").toArray();

 System.out.println("Missing formats");
 for (Object entry:array) {
 System.out.println(entry.toString().trim());

 String position= "";
 for(int i=0;i<array.length;i++) {
 if(!entry.toString().equalsIgnoreCase (array[i].toString())) {
 position= position+" "+i;
 }
 }
 System.out.println(position);
 }
 }

  public Multimap readFile() {
  File file = new File("/Users/sree/Desktop/text.txt");
  Multimap<String,String> dataMap =  ArrayListMultimap.create();

  ArrayList<String> al=new ArrayList<String>();
  BufferedReader br;

  try {
  br = new BufferedReader(new java.io.FileReader(file));
  Arrays.stream(br.readLine().split(",")).forEach(s ->al.add(s.trim()));
  String st;
  while ((st = br.readLine()) != null) {

  VariableIcrementor instance = new VariableIcrementor();
  Arrays.stream(st.split(",")).                                      
  forEach(s->dataMap.put(al.get(instance.increment()),s));
  }
  br.close();
  } catch (Exception e) {
  e.printStackTrace();
  }
  return dataMap;
  }

  public static void main(String[] args) {
       FileReader instance = new FileReader();
       instance.processData();
    }
}

variable incrementor implementation details

public class VariableIcrementor {

private int i=0;

public int increment() {
    return i++;
}
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302147 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO