-3

I'm parsing Outlook SCV file to ArrayList. Then i want to get String values of the Array list i'm getting.

Here is the code:

 if(arr != null){
        try{
            for (int i = 1; i < arr.size(); i++) {
           oneRow = new ArrayList();
            oneRow.add(arr.get(i));
                for (int j = 0; j < oneRow.size(); j++) {

                StringBuilder strBuild = new StringBuilder();
                    strBuild.append(String.valueOf(oneRow.get(j).toString()));

Here is the ArrayList: ArrayList

No matter what i tried, i can't get the String Value. What i get is : [Ljava.lang.string @....

Here is the Class that makes the ArrayList which getting the CSV file and building the ArrayList:

public class ReadingCSV {


InputStream inputStream;

public ReadingCSV(InputStream inputStream){
    this.inputStream = inputStream;
}

public List read(){
    ArrayList resultList = new ArrayList();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    try {
        String csvLine;
        while ((csvLine = reader.readLine()) != null) {
            String[] row = csvLine.split(",");
            resultList.add(row);
        }
    }
    catch (IOException ex) {
        throw new RuntimeException("Error in reading CSV file: "+ex);
    }
    finally {
        try {
            inputStream.close();
        }
        catch (IOException e) {
            throw new RuntimeException("Error while closing input stream: "+e);
        }
    }
    return resultList;
}
}
Igor Fridman
  • 1,267
  • 1
  • 16
  • 30
  • 2
    What is the purpose of: `String.valueOf(oneRow.get(j).toString())` ?? – mlewandowski Mar 09 '16 at 08:44
  • There is no purpose, i just trying to get the String value of ArrayList on [j] position. – Igor Fridman Mar 09 '16 at 08:47
  • Have you tried simply casting to String? – Shark Mar 09 '16 at 08:48
  • put the code to know exactly what kind of object this `arr` ? then only we will able to write Code for you – Vikrant Kashyap Mar 09 '16 at 08:48
  • I have something to say for all of these "nice" people who gave my question negatives feedback. It is very sad, because I think that the purpose of stackoverflow is to help people, and how a person will learn if he gets a negative feedback right away?! Instead of clicking that down arrow, maybe you should try to help. Many many Thanks for those, who really tried to help me. – Igor Fridman Mar 10 '16 at 09:30
  • Relax. Breathe slowly. Take a step outside... We've all been in your situation... I know that StackOverflow can feel like a very hostile place, especially if you're new here. Posting a complaint about this as an answer or comment is not the way to go, though. If you have any questions / remarks on the rules here at StackOverflow or the behavior of certain users, the proper procedure is to address them @ http://meta.stackoverflow.com/. – John Slegers Mar 10 '16 at 09:31

2 Answers2

2

Note :- This Code is compiled and run using jdk V1.8

try this out this is a working code. you can Manipulate as per your need.

public List<String> read(){
    ArrayList<String> resultList = new ArrayList(); //A Type-Safety (String) ArrayList
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    try {
        String csvLine;
        while ((csvLine = reader.readLine()) != null) {
            String[] row = csvLine.split(","); //row is String Array Object
            for(String eachWord : row) //Iterate each String from the array
            resultList.add(eachWord);  // add String to the Type-Safe ArrayList.
        }
    }catch (IOException ex) {
    throw new RuntimeException("Error in reading CSV file: "+ex);
}
finally {
    try {
        inputStream.close();
    }
    catch (IOException e) {
        throw new RuntimeException("Error while closing input stream: "+e);
    }
}
return resultList;
   }
  }

update Your another code with this

if(arr != null){
            for (int i = 1; i < arr.size(); i++) {
           ArrayList<String> oneRow = new ArrayList();
            oneRow.add(arr.get(i));
                for (int j = 0; j < oneRow.size(); j++) {
                    strBuild.append(oneRow.get(j));
                }
    }
    System.out.println(strBuild.toString());
}

This Code is perfectly Working. You may try it by your own. If any issue you may put a comment .

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • Vikrant Kashyap, Thank you so much, You are a genius. 3 days i'm trying to make it work. Unfortunately i can't give this answer a positive feedback. – Igor Fridman Mar 09 '16 at 11:14
0

Why are you double convert to String this:

strBuild.append(String.valueOf(oneRow.get(j).toString()));

I think,this is helps you: Help link

Community
  • 1
  • 1