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();
}
Related
Hey Guys My I am Dealing With This Issue
I Want To merge to line from txt file and show them in Arraylist
So My Code Is That And I Want To Skip The -- Line To Show in Arraylist..
private List getQuotes(){
List<String> quotes = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
InputStream open = getAssets().open("barish.txt");
bufferedReader = new BufferedReader(new InputStreamReader(open));
String line;
while ((line = bufferedReader.readLine()) != null) {
quotes.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return quotes;
}
[enter image description here][1]
txt File image is here
[1]: https://i.stack.imgur.com/AiI67.png
try to use replaceAll method then read the file as above
line = line.replaceAll("--"," ");
//to remove all "--" and replace it with a space
You could check if the line is not equal to -- before adding it to the quotes list.
if(line.equals("--") == false) {
quotes.add(line);
}
Edit: Combining the lines together
In order to combine the strings between the -- lines you could accumulate the quote into a string quoteLine between each --. So if the line is not a -- line then it will be appended to the string quoteLine. If it is a -- line then it will add the previously constructed quote to the array list and initialize quoteLine to an empty string to reset it.
String line;
String quoteLine = "";
while ((line = bufferedReader.readLine()) != null) {
if(line.equals("--") == false) {
quotes.add(quoteLine);
quoteLine = "";
} else {
quoteLine += line;
}
}
enter image description here
This is Txt File ..
This Is A Poetry App.. This 2 Lines Make 1 poetry
So I Want To Display every 2 lines together In My Recyclerlist View..
This The Example...
i Want To Display My Poetry Like This In Recycler View
enter image description here
I think perhaps you want something more like this:
public List<List<String>> getQuotes() {
List<List<String>> allQuotes = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
InputStream open = getAssets().open("barish.txt");
bufferedReader = new BufferedReader(new InputStreamReader(open));
String line;
ArrayList<String> quote = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("--")) {
if (!quote.isEmpty()) {
allQuotes.add(quote);
}
quote = new ArrayList<>();
} else {
quote.add(line);
}
}
if (!quote.isEmpty()) {
allQuotes.add(quote);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return allQuotes;
}
The result is a List<List<String>>: a list of poems, with each poem in its own List<String>.
You can do something like this:
public static List<List<String>> getQuotes(String file) {
List<List<String>> allQuotes = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
// Let's have a queue backed by LinkedList to keep the two
// lines just before the next line that is '--'.
Queue<String> quotes = new LinkedList<String>();
String line;
while ((line = br.readLine()) != null) {
// If the current line starts with -- and the two previous lines persisted
// in the quotes queue to allQuotes and clear the quotes. You can do an equals
// check here if you're certain about no leading and trailing white spaces in
// each line and it is always --.
if (line.trim().startsWith("--")) {
// Don't add quotes to allQuotes when quotes queue is empty.
// You can also check quotes.size() == 2 along with !quotes.isEmpty()
// if you want to always have at least two quotes in each sub-list retuned.
if (!quotes.isEmpty()) {
allQuotes.add(new ArrayList(quotes));
quotes.clear();
}
} else if (!line.trim().isEmpty()) {
// Add each line into the quotes queue if it is not blank
// or if it is not --
quotes.add(line);
}
// If the size of queue is > 2 remove an item from from the front,
// because we are only interested in two lines before --
if (quotes.size() > 2) {
quotes.remove();
}
}
} catch (Exception e) {
e.printStackTrace(); // TODO: Handle exceptions
return null;
}
return allQuotes;
}
I have a text file that contains the following:
example.txt
#ignore
#ignore line
#ignore line again
1234567
8940116
12131415
I want to read in the example.txt file into eclipse and add the data into a hashmap. I want the list to be arranged in numerical order and I want it to ignore any comments(any text with #) in the text file. I would like to print the hashmap as follows:
output:
1234567
8940116
12131415
You don't need a hashmap for storing just Strings. Maps are for key value pairs. If you want to put each line from file into a collection use Lists. ArrayLists, LinkedList maintain insertion order. You can use any of them. If you want sorted list you can use TreeList.
BufferedReader reader;
List<String> list = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(
"example"));
String line = reader.readLine();
while (line != null) {
if(!line.startsWith("#"){
list.add(line);
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Thr purpose of a Map is to store pairs Key/Value, for a single collection you may use a List it's far more efficient, the printing part is you job whatever the type of collection is
List<String> values = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("filename"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("#")) {
values.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
for (String v : values)
System.out.println(v);
I am having a problem with my program. What i am supposed to do is:
find all words from some txt files
store each word in array only once
Then sort alphabetically
I dont know how to ensure that each word won't appear twice(or more) in my array.
For example, a sentence from one of my files: My cat is huge and my dog is lazy.
I want the words "my" and "is" to appear only once in my array, not twice.
As for the sorting, is there anything that i can use from Java ? I don't know.
Any help is appreciated!
Here is what i have done so far:
try {
File dir = new File("path of folder that contains my files")
for (File f : dir.listFiles()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line = null;
while((line = br.readLine())!= null) {
String [] tokens = line.split(",\\s+|\\s*\\\"\\s*|\\s+|\\.\\s*|\\s*\\:\\s*");
}
}
}
Here is the modified code to have sorted unique words:
try {
TreeSet<String> uniqueSortedWords = new TreeSet<String>();
File dir = new File(
"words.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(dir)));
String line = null;
while ((line = br.readLine()) != null) {
String[] tokens = line
.split(",\\s+|\\s*\\\"\\s*|\\s+|\\.\\s*|\\s*\\:\\s*");
for(String token: tokens) {
uniqueSortedWords.add(token);
}
}
System.out.println(uniqueSortedWords);
//call uniqueSortedWords.toArray() to have output in an array
} catch (Exception e) {
e.printStackTrace();
}
ifI guess you are looking for a code something like this.
try {
ArrayList<String> list = new ArrayList<String>();
File dir = new File("path of folder that contains my files")
for (File f : dir.listFiles()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line = null;
while((line = br.readLine())!= null) {
String [] tokens = line.split(",\\s+|\\s*\\\"\\s*|\\s+|\\.\\s*|\\s*\\:\\s*");
for(int i=0; i<tokens.length(); i++)
{ //Adding non-duplicates to arraylist
if (!list.contains(tokens[i])
{
list.add(tokens[i]);
}
}
}
Collections.Sort(list);
}
}
catch(Exception ex){}
Do not forget: import java.util.*; at the beginning of your code to use Collections.Sort();
EDIT
Even though contains is a built-in method you can directly use with ArrayLists, this is how such a method works in fact (just in case if you are curious):
public static boolean ifContains(ArrayList<String> list, String name) {
for (String item : list) {
if (item.getName().equals(name)) {
return true;
}
}
return false;
}
then to call it:
ifContains(list, tokens[i]))
You can use the combination of HashSet and TreeSet
Hashset:hashset allows null object.
TreeSet:treeset will not allow null object,treeset elements are sorted in ascending order by default.
Both HashSet and TreeSet does not hold duplicate elements.
try {
Set<String> list = new HashSet<>();
File f = new File("data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line = null;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",\\s+|\\s*\\\"\\s*|\\s+|\\.\\s*|\\s*\\:\\s*");// other alternative:line.split("[,;-!]")
for (String token : tokens) {
list.add(token);
}
}
// Add the list to treeSet;Elements in treeSet are sorted
// Note: words must have the same case either lowercase or uppercase
// for sorting to work correctly
TreeSet<String> sortedSet = new TreeSet<>();
sortedSet.addAll(list);
Iterator<String> ite = sortedSet.iterator();
while (ite.hasNext()) {
System.out.println(ite.next());
}
} catch (Exception e) {
e.printStackTrace();
}
I am supposed to read a large file containing a string in each line. What is the best way to parse this kind of file in Java?
Currently I am parsing like this using BufferedReader:
public static List<String> readFile(String filename) {
List<String> input = new ArrayList<>();
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
input.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return input;
}
So my question here is, which one is a better way: to save like an array of String or an array of char arrays?
In other words, is it better to have too many String objects or too many char arrays?
EDIT:
I need to check if the string in each line is a palindrome or not.
In above scenario, you can use either of these. If you want change the content further, then you have to use array of char because String is immutable in nature in java.
I have a text file containing words separated by newline , like the following format:
>hello
>world
>example
How do i create an ArrayList and store each word as an element?
You can use apache commons FileUtils.readLines().
I think the List it returns is already an ArrayList, but you can use the constructor ArrayList(Collection) to make sure you get one.
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
File file = new File("names.txt");
ArrayList<String> names = new ArrayList<String>();
Scanner in = new Scanner(file);
while (in.hasNextLine()){
names.add(in.nextLine());
}
Collections.sort(names);
for(int i=0; i<names.size(); ++i){
System.out.println(names.get(i));
}
The simplest way is to use Guava:
File file = new File("foo.txt");
List<String> words = Files.readLines(file, Charsets.UTF_8);
(It's not guaranteed to be an ArrayList, but I'd hope that wouldn't matter.)
You read the file line-by-line, create an ArrayList for Strings, and add line.substring(1) to the defined ArrayList if line.length>0.
I put the file at "C:\file.txt"; if you run the following it fils an ArrayList with the words and prints them.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("C:\\file.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line != null) {
lines.add(line.replace(">", ""));
line = br.readLine();
}
for(String l : lines) {
System.out.println(l);
}
}
}
I'm sure they're lots of libraries that do this with 1 line, but here's a "pure" Java implementation:
Notice that we've "wrapped"/"decorated" etc. a standard FileReader (which only has read one byte at a time) with a BufferedReader which gives us a nicer readLine() method.
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream("test.txt"),
Charset.forName("ISO-8859-1")));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
System.out.println(lines);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}