0

I'm fairly new to java and I believe the problem comes from my lack of understanding.

I am trying to read an Array List of CSV values. Below is my code:

    public static void readFile(){
        String line = "";
        try{
            BufferedReader br = new BufferedReader(new FileReader(Main.filepath));

            while((line = br.readLine()) != null){  //reads whilst the next line is not null
                fileData.add(line);                 //adds current line to array list
        }
        catch (Exception ex){
            System.out.println("File Not Found");   //if file not found, print
        }
    }
public class Film{
    public static void duration(){
        readFile();
        for (int i = 0; i < (fileData.size()); i++) {
            //System.out.println(fileData);
            String[] temp = fileData.get(1).split(",");
            System.out.println(temp);
        }
    }

readFile() reads the CSV file and saves each line as an element in an ArrayList

filedata is the ArrayList

filepath is the path to the file

The CSV file is in format "filmName,releaseDate,filmRating,filmGenre,filmDuration,filmScore"

I'm trying to get Film() to print out the release dates of every element in the ArrayList, however my output is:

[Ljava.lang.String;@6bf2d08e
[Ljava.lang.String;@5eb5c224
[Ljava.lang.String;@53e25b76
[Ljava.lang.String;@73a8dfcc

When I'm expecting something more like

1999
2007
1956
1987

Any help is appreciated.

Jon_belt
  • 105
  • 1
  • 8

3 Answers3

2

Iterate over each String of the array tmp and print that value. Moreover, you should replace fileData.get(1).split(","); for fileData.get(i).split(",");

 for (int i = 0; i < (fileData.size()); i++) {
            String[] tmp = fileData.get(i).split(",");
            for(String t : tmp)
                System.out.println(tmp);
   }

If you System.out.println(); the array you will be printing basically, each String hashcode. Alternatively, you can use Arrays.toString to print the entire array in one go:

for (String fileDatum : fileData) {
    String[] tmp = fileDatum.split(",");
    System.out.println(Arrays.toString(tmp));
}

Finally, you use Streams from Java you can simplify your method to only:

 fileData.stream().map(i -> i.split(",")).forEach(i -> Arrays.asList(i).forEach(System.out::println));

or with a different formatting style:

  fileData.stream().map(i -> i.split(",")).map(Arrays::toString).forEach(System.out::println);
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0
String[] temp = fileData.get(1).split(",");
System.out.println(temp);

This prints out the Array with the default implementation of toString:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

In your case you need to iterate over the Array again and spring each element with toString

Vetemi
  • 784
  • 2
  • 9
  • 26
0

You have to first reefer to i variable instead of 1 in your loop, then the split method returns an array of String, so you should select the correct value in the array :

 String[] temp = fileData.get(i).split(",");
 System.out.println(temp[1]);
Fabien MIFSUD
  • 335
  • 5
  • 14