0

I'm trying to read CSV file like this:

1,0,0,0,2,0,0,2,
0,2,0,0,0,0,0,5,
0,0,0,0,0,0,5,0,

This is my expected outcome:

[1, 5, 8]
[2, 8,]
[7]

This is my Java coding:

CSVReader a = new CSVReader(new FileReader("CM.csv"));
List<String[]> aa = a.readAll();

List<Integer> list = new ArrayList<>(); 
Object[] CM = new Object[3];

for (int i = 0; i < aa.size(); i++) {
    for (int x = 0; x < aa.get(i).length-1; x++) {
        if ( Integer.parseInt(aa.get(i)[x].trim()) >= 1 ){
                list.add(x+1);
            }
        }            
        CM[i] = list;
        list.clear();
    }

for (int i = 0; i < CM.length; i++) {
    System.out.print(CM[i]);
    System.out.print("\n");
    }

But I get Null outcome. If I delete the list.clear() line then get weird outcome like this :

[1, 5, 8, 2, 8, 7]
[1, 5, 8, 2, 8, 7]
[1, 5, 8, 2, 8, 7]

I want to store the list into the array, please ignore the way how i read the CSV file...

12
  • 1
    Two things: 1. How do you actually get to that output? 2. You should use better variable names than a and aa. Commented Oct 9, 2014 at 17:28
  • This doesn't make sense to me? Commented Oct 9, 2014 at 17:28
  • What are you trying to do? Commented Oct 9, 2014 at 17:28
  • Re: BradyK If i know i no need to ask here Commented Oct 9, 2014 at 17:30
  • Explain what you are trying to do so we can help you...u have the excel input now what do you want to do with the excel input? Commented Oct 9, 2014 at 17:31

2 Answers 2

1

replace list.clear(); with list = new ArrayList<>();

you are assigning list to CM[i] and when you are calling list.clear() it clears the list and makes it empty list, as your CM variable is referring to list object, it wont have any values in it.

To get the values:

for (int i = 0; i < CM.length; i++) {
    List dataList = (List) CM[i];
    for (int j = 0; j < dataList.size(); j++) {
        // do your stuff
        System.out.println(dataList.get(j));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

1 more question, how can i access the list value of the each array CM?? if i use CM[i].list[j] it does't work
for (int i = 0; i < CM.length; i++) { List datalist = (List)CM[i]; for (int j = 0; j < datalist.size(); j++) { System.out.print(CM[i].get(i)[j]); } } I do like this but can't......
I'm not prefer to create a new listdata. But can make it access to the CM[i] and list? like CM[i].list[i]
here you are not creating any new list object, you are just referring to the existing list object. in that case you can use System.out.print(CM[i].get(j)); with in second loop
1
public class Location {
   private int x = 0;
   private int y = 0;

   public Location(int x, int y)
   {
       this.x = x;
       this.y = y;
   }

   public int getX()
   {
       return this.x;
   }
   public int getY()
   {
       return this.y.
   }

}


CSVReader csvReader = new CSVReader(new FileReader("CM.csv"));
List<String[]> records = csvReader.readAll();

ArrayList<Location> locations = new ArrayList<Location>();

for(int i = 0; i < records.size(); i++)
{
    for(int j = 0; j < records.get(i).length; j++)
    {
        if(Integer.parseInt(records.get(i)[j]) > 0)
        {
             locations.add(new Location(i,j));
        }
    }
}

Now all your locations will be stored in the locations arraylist. All you have to do is loop through that and get your coordinates for the records arraylist.

9 Comments

My question is focus on how to store the list into the array, not shortcut my coding, pls ignore how i read the input
This is exactly how you store the list into an array. This answers your question try the code.
read again my question, i have edited maybe u can see the diff
I've read it several times it makes no sense what you are trying to do.
If you want to store the locations where the number is greater than 1 then just create a location class with x and y and store those objects in an arraylist. Your question is very unclear as to what you are trying to do or why you are trying to do it. Based on what you said in the comments this answer is correct. You need to be more clear about what you are asking then we can better help you.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.