0

I'm going to do a Java library on a simple data frame that can read CSV files, edit the CSV and export CSV file. My problem is on how to export it.
Here is how I read my CSV file:

String line;
List dataFrame = new ArrayList();
String filePath = "C:\\Users\\user\\Desktop\\SimpleDataFrame\\src\\Book1.csv";

try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
    while ((line = br.readLine()) != null) {
         List values = Arrays.asList(line.split(String.valueOf(",")));
         dataFrame.add(values);
      }
 } catch (Exception e) {
         System.out.println(e);
 }

And this is how I implement the write CSV file:

 FileWriter writer = new FileWriter(new File("Book2.csv"));
 for(int i = 0; i<dataFrame.size(); i++){
     String[] array = (String [])dataFrame.get(i);
     for(int j = 0; j<array.length; j++){
         writer.write(array[j]);
         if(j<array.length-1) writer.write(",");
         else writer.write("\n");
     }
 }

And this is the exception that it throws to me:

Exception in thread "main" java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class [Ljava.lang.String; (java.util.Arrays$ArrayList and [Ljava.lang.String; are in module java.base of loader 'bootstrap')

Can I know what is the problem?

616Hz_lim
  • 21
  • 6

2 Answers2

1

In one method you have:

List values = Arrays.asList(line.split(String.valueOf(",")));
     dataFrame.add(values);

So here values is a List

Then in the write method you have:

String[] array = (String [])dataFrame.get(i); 

String[] is different from List that's why you have an error when you try to cast it.

It would be better if in top you do:

  List<String> values = Arrays.asList(line.split(String.valueOf(",")));
     dataFrame.add(values);

Adding generics. Then in the write method something like:

List<String> stringList = (List<String>) dataFrame.get(i); //Add generics to data frame here so you don't need that cast!
for(int j = 0; j<stringList.size(); j++){
     writer.write(stringList.get(j));
     if(j<stringList.size()-1) writer.write(",");
     else writer.write("\n");
 }
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • Thanks for helping it solves the exception!! But can I ask why is the new CSV file created is empty?? – 616Hz_lim Dec 03 '20 at 15:19
  • @616Hz_lim it shouldn't be empty - perhaps you've forgotten to close the file? See my answer, I've wrapped it in a try-with-resources to take advantage of the fact that `FileWriter` is `AutoCloseable`. – Hulk Dec 03 '20 at 15:59
  • Ohh I see where's my wrong. Can I also add a `writer.close()` ?? – 616Hz_lim Dec 04 '20 at 02:22
  • @616Hz_lim yes, but in that case you should put `writer.close()` in a `finally` block, otherwise it would not be closed correctly if there is an exception when writing. That is what we did before try-with-resources was introduced. See also https://stackoverflow.com/questions/26516020/try-with-resources-vs-try-catch – Hulk Dec 04 '20 at 06:47
0

The problem here is your use of rawtypes. Lists in java are generic, and you have to specify the element type you want to use. Also, Lists are not interchangeable with arrays, and cannot directly cast to arrays.

List dataFrame = new ArrayList();

should trigger a warning, and you should not ignore this warning.

Based on your usage, it has to be a

List<List<String>> dataFrame = new ArrayList<>();

The elements are Lists, because you explicitly convert them from arrays to Lists in

List values = Arrays.asList(line.split(String.valueOf(",")));

String.split returns a String[], which you convert to a List with Arrays.asList.

This line should be

List<String> values = Arrays.asList(line.split(","));

(you don't need String.valueOf here, the literal is already a String).

And then in

String[] array = (String [])dataFrame.get(i);

you get a runtime exception because dataFrames contains Lists, not arrays String[]. This can be rewritten as, for example using enhanced for loop and String.join, and wrapping the writer in a try-with-resources, so that you can't forget closing it.

try (FileWriter writer = new FileWriter(new File("Book2.csv"))) {
    for (List<String> list : dataFrame) {
        writer.write(String.join(",", list));
        writer.write("\n");
    }
} catch (IOException e) {
    e.printStackTrace();
}
Hulk
  • 6,399
  • 1
  • 30
  • 52
  • If like your implementation, how can I access the list of String inside the dataFrame List? I do not know how to access a two dimensional List – 616Hz_lim Dec 03 '20 at 15:20
  • you can just use `dataFrame.get`, if you want to - it will return a `List`. – Hulk Dec 03 '20 at 15:22
  • So means that I can save the List into another List and then use `.get` ?? – 616Hz_lim Dec 03 '20 at 15:25
  • Basically, yes. You can store `List`s in a `List>`, ang get them out with `.get(index)` – Hulk Dec 03 '20 at 15:42