Skip to content
Advertisement

Trouble getting and printing a returned value from one class to another in Java

I’m trying to import data from a CSV file. I don’t have any issue about the importing process. But, when I’m passing an imported array from the CSV file to another method in another class, I can not get and print the values from the array. I got the following printing: [1, 2.0, [[Ljava.lang.String;@1e81f4dc]. “1” and “2” are imported values and are o.k.

The real trouble for me is to get and print the values in “[Ljava.lang.String;@1e81f4dc]”.

I’d appreciate if you can help to know how to get the values in [Ljava.lang.String;@1e81f4dc] and print them. The array in [Ljava.lang.String;@1e81f4dc] is a 2d array with double values.

The code I’m working on is the next one:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

class importCsvToJava{ 

  public static List<Object> getExample( ){
    List<String[]> rowList = new ArrayList<String[]>();
    try (BufferedReader br = new BufferedReader(new FileReader("/Users/user1/Desktop/csv1.csv"))) {
        String line;
        while ((line = br.readLine()) != null) {
            String[] lineItems = line.split(";");
            rowList.add(lineItems);
        }
        br.close();
      }
    catch(Exception e){        
    }
  
    String[][] matrix = new String[rowList.size()][];
    for (int i = 0; i < rowList.size(); i++) {
        String[] row = rowList.get(i);
        matrix[i] = row;         
    } 
    
    int a=1;
    int b=2;        
    return Arrays.asList(a,b,matrix);

  }
}  
      

public class Callback {
  public static void main(String[] args  ) {
    
    try{importCsvToJava.getExample();
  
      List<Object> data = importCsvToJava.getExample();
      System.out.println("Returned_matrix:"+data);

    
    } catch (Exception e) {
      System.out.println("Error during optimization");
      e.printStackTrace();
      
    } 
  }    
}

Advertisement

Answer

String[][] mat = (String[][]) data.get(2);

for(String[] strings:mat)
{
  for(String st:strings){
    System.out.println(st);
   }  
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement