Splitting by Tabs in Java
Solution 1
The split
method returns an array, not an ArrayList
.
Either work with an array or convert it to an ArrayList
manually.
Solution 2
x.split("\t");
this function will return an array not array list
The split function states that:
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
You may try to change your code like this:
ArrayList<String> voteSubmission = new ArrayList<String>();
for(String x : fileRead)
{
for(String value: x.split("\t"))
{
voteSubmission.add(value);
}
}
csharpener
Updated on June 11, 2022Comments
-
csharpener almost 2 years
I have a file scanned line by line into an ArrayList. I then create a new ArrayList in which I want to temporarily store that line into so that I may access certain values.
Ex. IDname(tab)IDnumber(tab)vote(tab)date
So, I create the temporary ArrayList named voteSubmission, and I go through every String in the fileRead array.
Why is it that I get the error incompatible type for my split method?
ArrayList<String> voteSubmission = new ArrayList<String>(); for(String x : fileRead) { voteSubmission = x.split("\t"); }