Merge pull request #404 from divyeshjagatiya/master

Good Work! Thanks for contribution.
This commit is contained in:
Christian Bender 2018-04-04 23:46:23 +02:00 committed by GitHub
commit 8dcc57f9b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,6 +73,8 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -97,37 +99,7 @@ public class CSVFile {
* @purpose loads the CSV-file and fills the inner table with the data * @purpose loads the CSV-file and fills the inner table with the data
*/ */
public CSVFile(String path, char seperator) { public CSVFile(String path, char seperator) {
table = new ArrayList<ArrayList<String>>(); this(new File(path),seperator);
trackList = new ArrayList<Integer>();
pathCSVFile = path;
this.seperator = seperator;
String row = null;
File file = new File(path);
ArrayList<String> colums = new ArrayList<String>();
if (!file.canRead() || !file.isFile()) {
System.out.println("unable to open file");
System.exit(1);
}
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(path));
while ((row = in.readLine()) != null) {
// uses the compile method to compile the row
// in its columns.
table.add(compile(row, seperator));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
@ -141,32 +113,17 @@ public class CSVFile {
trackList = new ArrayList<Integer>(); trackList = new ArrayList<Integer>();
pathCSVFile = file.getPath(); pathCSVFile = file.getPath();
this.seperator = seperator; this.seperator = seperator;
String row = null;
ArrayList<String> colums = new ArrayList<String>(); ArrayList<String> colums = new ArrayList<String>();
if (!file.canRead() || !file.isFile()) { if (!file.canRead() || !file.isFile()) {
System.out.println("unable to open file"); System.out.println("unable to open file");
System.exit(1); System.exit(1);
} }
BufferedReader in = null;
try { try (BufferedReader br = Files.newBufferedReader(Paths.get(file.getAbsolutePath()))) {
in = new BufferedReader(new FileReader(file)); br.lines().forEach(line -> table.add(compile(line, seperator)));
while ((row = in.readLine()) != null) {
// uses the compile method to compile the row
// in its columns.
table.add(compile(row, seperator));
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
} }
@ -275,14 +232,14 @@ public class CSVFile {
return columns; return columns;
} }
private static Pattern PATTERN_PUNCTUATION = Pattern.compile("\\p{Punct}");
/** /**
* *
* @param ch * @param ch
* @returns true if ch is punctuation character otherwise false. * @returns true if ch is punctuation character otherwise false.
*/ */
public static boolean isPunctuation(char ch) { public static boolean isPunctuation(char ch) {
return Pattern.matches("\\p{Punct}", "" + ch); return PATTERN_PUNCTUATION.matcher("" + ch).matches();
} }
@ -489,18 +446,16 @@ public class CSVFile {
* @returns true if a row contains 'key' otherwise false. * @returns true if a row contains 'key' otherwise false.
*/ */
public boolean contains(String key) { public boolean contains(String key) {
boolean ans = false;
key = key.trim(); key = key.trim();
for (int i = 0; i < table.size(); i++) { for (int i = 0; i < table.size(); i++) {
for (String item : table.get(i)) { for (String item : table.get(i)) {
item = item.trim(); item = item.trim();
if (item.equals(key)) { if (item.equals(key)) {
ans = true; return true;
break;
} }
} }
} }
return ans; return false;
} }