Importing csv in android application - java

I've found this code on a previously answered question to import a csv in an android application, i have to import into my android application a file called data.csv, i then need to perform searches on said database, the rows as such:
178464;AA1
...
here's the code:
package com.cdac.qrcodescanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVFile {
InputStream inputStream;
public CSVFile(InputStream inputStream){
this.inputStream = inputStream;
}
public List read(){
List resultList = new ArrayList();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(";");
resultList.add(row);
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
finally {
try {
inputStream.close();
}
catch (IOException e) {
throw new RuntimeException("Error while closing input stream: "+e);
}
}
return resultList;
}
}
and here's what goes where i want to use my data:
InputStream inputStream = getResources().openRawResource(R.raw.data);
CSVFile csvFile = new CSVFile(inputStream);
List scoreList = csvFile.read();
i now want to use the get method to have the string after the semicolon (the value), and so i tried:
String i = scoreList.get(178464);
That prompts me with the error:
incompatible types:
Required: Java.lang.string
Found: Java.lang.Object
doing this fixed the error but made my application crash:
String i = (string)scoreList.get(178464);
Object i = scoreList.get(178464);
I'm not particularly expert with Java, but i have the feeling i'm doing something incredibly wrong and i can't for the life of me figure out what, any help would be appreciated!

In the CSVFile.read() method, we have the following :
String[] row = csvLine.split(";");
resultList.add(row);
The resultList will be a list of arrays of strings. Each element of the list is a row of your file. Each element of the array is a value (column).
So...
String i = (string)scoreList.get(178464);
returns the 178464th line of the file. I'm assuming it will crash because you do not have 178464 lines in your files, and even if you did, the value returned should be a String[].
I'm assuming you wish to index each element by it's id (178464 in your example) and access it using this id. You should use a Map instead.
Map resultMap = new HashMap();
while ((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(";");
String id = row[0];
Integer score = Integer.parseInt(row[1]);
resultMap.put(id, score);
}
Then you can access the map using the ids :
Integer score = resultMap.get("178464");

Related

Best way to use CSV files in Java

Im having trouble with the best approach to reading a CSV file in order to extract and compare certain things in it. The file is made up of strings, and I need to keep track if there are duplicated items. Here is what I have so far.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVReader {
public static void main(String[] args) {
String csvFile = "Cchallenge.csv";
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
} catch (IOException e) {
e.printStackTrace();
}
}
}
So I made an array called country with all the data. But when I go to print out the arrays length, it gives my a lot of different arrays with varying sizes. I am having a hard time traversing the arrays and extracting the duplicates. Any ideas will help, thanks.
If you simply wish to get a list of the items without any duplicates, then you could collect the items into a set, as sets do not allow duplicate items:
Set<String> items = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
items.addAll(Arrays.asList(line.split(cvsSplitBy)));
}
} catch (IOException e) {
e.printStackTrace();
}
If you also want to keep track of the duplicates, you could use another set and add items into it if they already exist in the first set. This would be an easy feat to accomplish, as the add method of Set returns a boolean in regards to if the set already contained the specified element or not:
Set<String> items = new HashSet<>();
Set<String> duplicates = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
for (String item : line.split(cvsSplitBy)) {
if (items.add(item)) {
continue;
}
duplicates.add(item);
}
}
} catch (IOException e) {
e.printStackTrace();
}

Buffered Reader, Line count, Split, Parse in Java

Actually, I am assigned a task where I have a xyz.txt/CSV file which will basically have numeric values and I am supposed to pass it through BUFFERED READER then split those values and finally parse them.
So I have a Java code can body help me with it.
package javaapplication12;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class JavaApplication12 {
public static void main(String[] args) {
String count= "F:\\Gephi\\number.txt";
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(count);
br = new BufferedReader
// AT THIS POINT THERE SHOULD BE SOME THING THAT COUNTS NUMBER OF LINES USING COUNT++ OR SOMETHING LIKE THIS//
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
// COMING TO THIS POINT THE ABOVE VALUES OF .TXT FILE SHOULD BE SPLIT USING SPLIT PARAMETER//
// AFTER SPLITTING THE SPLIT VALUE SHOULD BE KEPT IN AN ARRAY AND THEN EVENTUALLY PARSED//
Or IF Anybody can rewrite the code in another way of the above-stated problem, it shall also be appreciated.
Here is my solution with Java 8:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class BR {
public static void main(String[] args) {
String fileName = "br.txt";
//for the csv format
String regex = ", ";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
List<String[]> lines = br.lines()
.map(line -> line.split(regex))
.collect(Collectors.toList());
parse(lines);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void parse(List<String[]> lines) {
//Do your stuff here
}
}
The initialization of the BufferedReader is in the try block (this approach is called try with resources), since BufferedReader implements AutoCloseable (an interface), so in case an exception is thrown the reader will close.
The br.lines() method returns all lines from the file.
In the map function you are passing a line that is rad in a lambda. The line is split using the split variable (for CSV format it is ', ') and is returned and collected.
The result is a List of arrays of String which can be changed in the body of the map function.
For more clarification I suggest you check some Java 8 tutorials and you will fully understand what is going on.
This solution might not be appropriate for your knowledge level (I guess), but hope it inspires you to check some fancier and modern approaches.
Have a nice day.

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

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.
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 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])

How to get data from csv file based on first column name using java

In attached csv image I am trying to fetch the data and store it hashmap based on testcase name which is in first column. And in second column I have given rowcount value which says number of test data available for that particular testcase, so that I can get the value of rowcount and loop it for that many times to get all those data available for that particular testcase. I have written the code to get the value of rowcount. But I do no how to fetch the data for that particular test and store it in hashmap.
Click here to view CSV File Image
String path = "./TestData.csv";
String TestCaseName="Login";
String CurrentLine;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((CurrentLine = br.readLine()) != null) {
String Data[] = CurrentLine.split(",");
if(TestCaseName.equalsIgnoreCase(Data[0])){
System.out.println("Details in this row :"+ " " +CurrentLine);
String rowcount_value = Data[1];
System.out.println(rowcount_value);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
In the attached csv image I wanted to fetch data for Testcase Login, So I gave Testcase name as Login in the above code. And now I want to fetch all the highlighted data in the image which belongs to Login testcase and store it in a hashmap. The above code just gives the rowcount value of the passed testcase name.
Please help me to fetch those highlighted data and store it in hashmap. I am new to this field and Thanks in advance.
As your csv file is having fixed number of data points for different test cases, you may need to update the below code in case of adding more test cases.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ReadCsv {
public static void main(String[] args) {
String path = "sample.csv";
// String TestCaseName="Login";
String CurrentLine;
//Map to store TestCase -> Map(Key ->value)
Map<String,Map<String,String>> testcaseMap = new HashMap<String,Map<String,String>>();
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
br.readLine();
while ((CurrentLine = br.readLine()) != null) {
String Data[] = CurrentLine.trim().split(",");
String testCase_DataNames = "";
for(int j=2;j<Data.length;j++){
testCase_DataNames = testCase_DataNames +"_"+Data[j];
}
int dataCount = Integer.parseInt(Data[1]);
Map<String,String> testdata = new HashMap<String,String>();
for(int i=0;i<dataCount;i++){
String nextLine = br.readLine();
String feilds[] = nextLine.trim().split(",");
testdata.put(feilds[2], feilds.length ==4 ? feilds[3]:null);
}
testcaseMap.put(Data[0]+testCase_DataNames, testdata);
/*if(TestCaseName.equalsIgnoreCase(Data[0])){
System.out.println("Details in this row :"+ " " +CurrentLine);
String rowcount_value = Data[1];
System.out.println(rowcount_value);
} */
}
for(Map.Entry<String,Map<String,String>> entry:testcaseMap.entrySet()){
String key[] = entry.getKey().split("_");
System.out.println("Test case for:"+key[0]);
for(Map.Entry<String, String> dataPair: entry.getValue().entrySet()){
System.out.println("Data header:"+key[1]+" with value:"+dataPair.getKey());
if(dataPair.getValue()!= null ) {
System.out.println("Data header:"+key[2]+" with value:"+dataPair.getValue());
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Test case for:Login
Data header:UserName with value:uname2
Data header:Password with value:pwd2
Data header:UserName with value:uname1
Data header:Password with value:pwd1
Test case for:Hotel
Data header:HotelNames with value:Ambur
Data header:HotelNames with value:BlackPerl
Data header:HotelNames with value:Zingro
Hope this will work for you ! Good luck

adding objects to java queues from a data file

I am trying to add objects to a queue from a data file which is made up of text which is made up of a person's first name and their 6 quiz grades (ie: Jimmy,100,100,100,100,100,100). I am accessing the data file using the FileReader and using BufferReader to read each line of my data file and then tokenize each line using the "," deliminator to divide the names and quiz grades up. Based on what I think my professor is asking for is to create a queue object for each student. The assignment says,
Read the contents of the text file one line at a time using a loop. In this loop, invoke the processInputData method for each line read. This method returns the corresponding Student object. Add this student object to the studentQueue.
If someone could point me the right direction that would be great! Here is my code so far:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
// Create an empty queue of student objects
LinkedList<Student> studentQueue;
studentQueue = new LinkedList<Student>();
// Create an empty map of Student objects
HashMap<String, Student> studentMap = new HashMap<String, Student>();
System.out.printf("Initial size = %d\n", studentMap.size());
// Open and read text file
String inputFileName = "data.txt";
FileReader fileReader = null;
// Create the FileReader object
try {
fileReader = new FileReader(inputFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// BufferReader to read text file
BufferedReader reader = new BufferedReader(fileReader);
String input;
// Read one line at a time until end of file
try {
input = reader.readLine();
while (input != null) {
processInputData(input);
input = reader.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
// Close the input
try {
fileReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
// Tokenize the data using the "," as a delimiter
private static void processInputData(String data) {
StringTokenizer st = new StringTokenizer(data, ",");
String name = st.nextToken();
String homework1 = st.nextToken();
String homework2 = st.nextToken();
String homework3 = st.nextToken();
String homework4 = st.nextToken();
String homework5 = st.nextToken();
String homework6 = st.nextToken();
// Using the set methods to correspond to the Student object
Student currentStudent = new Student(name);
currentStudent.setHomework1(Integer.parseInt(homework1));
currentStudent.setHomework2(Integer.parseInt(homework2));
currentStudent.setHomework3(Integer.parseInt(homework3));
currentStudent.setHomework4(Integer.parseInt(homework4));
currentStudent.setHomework5(Integer.parseInt(homework5));
currentStudent.setHomework6(Integer.parseInt(homework6));
System.out.println("Input File Processing...");
System.out.println(currentStudent);
}
}
One possible solution to your problem is returning the student in processInputData(..)
private static Student processInputData(String data) {
// the same code
return currentStudent;
}
And in while loop
while (input != null) {
studentQueue.add(processInputData(input));
input = reader.readLine();
}
Also try to manage better your try-catch blocks, cause if your fileReader throws exception then the code will continue running and throw probably a nullPointerException that you don't handle.
try{
fileReader = new FileReader(inputFileName);
BufferedReader reader = new BufferedReader(fileReader);
}catch(IOException ex){
//handle exception;
}finally{
// close resources
}

Categories