Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 10 additions & 55 deletions Data Structures/CSVFile/src/CSVFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;
Expand All @@ -97,37 +99,7 @@ public class CSVFile {
* @purpose loads the CSV-file and fills the inner table with the data
*/
public CSVFile(String path, char seperator) {
table = new ArrayList<ArrayList<String>>();
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();
}
}
}

this(new File(path),seperator);
}


Expand All @@ -141,32 +113,17 @@ public CSVFile(File file, char seperator) {
trackList = new ArrayList<Integer>();
pathCSVFile = file.getPath();
this.seperator = seperator;
String row = null;
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(file));
while ((row = in.readLine()) != null) {
// uses the compile method to compile the row
// in its columns.
table.add(compile(row, seperator));
}

try (BufferedReader br = Files.newBufferedReader(Paths.get(file.getAbsolutePath()))) {
br.lines().forEach(line -> table.add(compile(line, seperator)));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


Expand Down Expand Up @@ -275,14 +232,14 @@ public static ArrayList<String> compile(String row, char sep) {
return columns;
}


private static Pattern PATTERN_PUNCTUATION = Pattern.compile("\\p{Punct}");
/**
*
* @param ch
* @returns true if ch is punctuation character otherwise false.
*/
public static boolean isPunctuation(char ch) {
return Pattern.matches("\\p{Punct}", "" + ch);
return PATTERN_PUNCTUATION.matcher("" + ch).matches();
}


Expand Down Expand Up @@ -489,18 +446,16 @@ public ArrayList<String> findRow(String key) {
* @returns true if a row contains 'key' otherwise false.
*/
public boolean contains(String key) {
boolean ans = false;
key = key.trim();
for (int i = 0; i < table.size(); i++) {
for (String item : table.get(i)) {
item = item.trim();
if (item.equals(key)) {
ans = true;
break;
return true;
}
}
}
return ans;
return false;
}


Expand Down