Owen Owen - 3 years ago 207
Java Question

Using .split(","); to split .csv data into an array whilst in a for loop?

This is a problem I am currently having. Here is my code:

String[] sArray = new String[maxEntries*2];

for (int i = 0; i != (maxEntries*2); i++)
sArray[i] = lines[i].split(",");

And lines[] is an array that has already read the data from the .csv file
The previous code WOULD work if I replaced 'i' with 0, for example, but I need to split the data for the entire lines[] array.
However, I get the following error message repeatedly:

error: incompatible types: String[] cannot be converted to String
sArray[i] = lines[i].split(",");

any help would be much appreciated!

Answer Source

sArray is an array of String, thus sArray[i] is a String. The split(String str) method returns an array.

You are trying to give an array value to a string variable. sArray must be an array of String arrays

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download