0

I have some code but i need help figuring out how to save the results of that if statement to an array, then saving it to a .csv file. Thanks in advance!

String data = new Scanner( new File("Test.csv") ).useDelimiter("\\A").next();
String terms = new Scanner( new File("Terms.txt") ).useDelimiter("\\A").next();

data.split(",");
terms.split("\n");

if (sourceElement.indexOf(dictionaryElement) > 0)
{
//Save results here
}
NameOfArray.join(",");
//Here I need to write the array to a .csv file... Help?

Also, I'm trying to use a text file full of keywords to search a csv file and save the results to a new csv file... Will this script even work? I'm fairly new to coding...

2
  • Can you provide the some sample sample keywords and some sample text from csv? Also what is the "result" that you want to write to the new file? Commented Nov 21, 2014 at 13:50
  • stackoverflow.com/questions/27056065/… This is what im trying to do. Ignore that last scrpit.. It didnt work Commented Nov 21, 2014 at 13:58

3 Answers 3

2

Try using this code:

//imports
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

//create the array lists to save the data
private static List<String> readData = new ArrayList<String>();
private static List<String> readTerms = new ArrayList<String>();
private static List<String> splitData = new ArrayList<String>();
private static List<String> finalData = new ArrayList<String>();

public static void main(String[] args) throws Exception{
   readData();
   processData();
   writeFinalData();
}

public static void readData() throws Exception{
//create readers
BufferedReader data = new BufferedReader(new FileReader(new File("Test.csv")));
BufferedReader term = new BufferedReader(new FileReader(new File("Terms.txt")));

//read the data and save it into the array lists
String line;
while((line = term.readLine()) != null){
   readTerms.add(line);
}

while((line = data.readLine()) != null){
   readData.add(line);
}

//close the readers
data.close();
term.close();
}

Now we have two array lists witch hold the data from the .csv file and the data from the terms file. The next step is to process this data and write it to a file.

public static void processData(){
   //looping through every line in the .csv file, spliting it by , and saving every     piece of the data into a new array list
   for(String d : readData){
      String[] split = d.split(",");
      for(int i = 0; i < split.length; i++){
         splitData.add(split[i]);
      }
   }

//searching the .csv file for keywords
for(String s : splitData){
   for(String k : readTerms){
      //testing if the data from the .csv file contains (or is equal to) the keyword. If true then we add it to the array list which holds the final data that will be writen back to the file
      if(s.contains(k)){
         finalData.add(s);
      }
   }
}
}

The last thing left to do is to write the data that matches the keywords back to a file.

public static void writeFinalData() throws Exception{
   BufferedWriter bw = new BufferedWriter(new FileWriter(new File("words.txt")));

   for(String s : finalData){
      bw.write(s);
      bw.newLine();
   }

   bw.close();
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the reply! Is there any way to write this in JavaScript rather than Java so I can call it from a batch file and run it?
@AmarokStudios Sorry, I have no idea... But you can run .jar or .class files from batch also. For .jar use: java -jar [your jar file name].jar and for .class use: java [your class name]
Okay, awesome. I'll give it a shot! I'll come back and let you know if it works and accept your answer if it does. Thanks :D
@AmarokStudios You need to import them! If you're using eclipse you can use Ctrl + Shift + o to fix imports. If you're using netbeans press Ctrl + Shift + I.
@AmarokStudios or just copy and paste: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List;
|
0

Yes, you can use this loop instead:

   public static void writeFinalData() throws Exception{
   BufferedWriter bw = new BufferedWriter(new FileWriter(new File("words.txt")));
   String finalLine = "" ; 

   //merge all the data into one line and separate it by “,“
   for(String s : finalData){
      finalLine += s + ","; 
   }

   //remove the comma at the end of the line
   finalLine = finalLine.substring(0, finalLine.length() - 1);

   bw.write(finalLine);

   bw.close();
 }

1 Comment

Okay... one more question. I have multiple rows of data so I would like to search them row by row. Is there a way to do that? Right now it's saving the data in a single row.
0

Here you go. I had to change the whole program. :) Don't forget the imports again ;)

// create the array lists to save the data
private static List<String> readTerms = new ArrayList<String>();

private static BufferedWriter bw;

public static void main(String[] args) throws Exception {
    openWriter();
    readData();
    closeWriter();
}

public static void readData() throws Exception {
    // create readers
    BufferedReader data = new BufferedReader(new FileReader(new File(
            "Test.csv")));
    BufferedReader term = new BufferedReader(new FileReader(new File(
            "Terms.txt")));

    // read the data and save it into the array lists
    String line;
    while ((line = term.readLine()) != null) {
        readTerms.add(line);
    }

    while ((line = data.readLine()) != null) {
        processLine(line);
    }

    // close the readers
    data.close();
    term.close();
}

public static void processLine(String line) throws Exception{
    String[] splitLine = line.split(",");
    String finalLine = "";
    for(int i = 0; i < splitLine.length; i++){
    for(String k : readTerms){
        if(splitLine[i].contains(k)){
            finalLine += splitLine[i] + ",";
        }
    }
    }
    finalLine = finalLine.substring(0, finalLine.length() - 1);
    saveLine(finalLine);
}

public static void saveLine(String line) throws Exception{
    bw.write(line);
    bw.newLine();
}

public static void openWriter() throws Exception{
    bw = new BufferedWriter(new FileWriter(new File("words.txt")));
}

public static void closeWriter() throws Exception{
    bw.close();
}

3 Comments

Thanks a lot for the help... I have the imports but it's telling me I have a syntax error on tokens... :/
1. check your imports: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List;
2. Check if you've put the code inside the class. public class [TheNameOfYourClass]{ [the code] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.