shilps
shilps

Reputation:

error in java programming:

paramList = new ArrayList<String>();
paramList.add(line.split(","));

When I used this, it gives me the error:

cannot find symbol
symbol:   method add(java.lang.String[])
location: interface java.util.List<java.lang.String>

With this, I would get output in this manner: "abc" "bvc" "ebf" . Now can I use trim to get rid of " ". So that the output becomes: abc bvc ebf

Input: "AAH196","17:13:00","02:49:00",287,166.03,"Austin","TX","Virginia Beach","VA"

Output: AAH196 17:13:00 02:49:00 287 166.03 Austin TX Virginia Beach VA

I need to remove the " "around the words and , between the words. I want to store this output and then jdbc will parse this data into the tables of my database on mysql.

Upvotes: 0

Views: 119

Answers (2)

Sandro
Sandro

Reputation: 2259

paramList.add() wants a String but line.split(",") returns String[]

The two are not equivalent.

Maybe you want something like:

paramList = Array.asList(line.split(","));

or

paramList = new ArrayList<String>();
for(String s : line.split(",")){
  paramList.add(s);
}

As for the added question, there are lots of ways to skin a cat.

If the words are ALWAYS surrounded by quotes then you can do something like:

paramList = new ArrayList<String>();
for(String s : line.split(",")){
  paramList.add(s.substring(1, s.length());
}

This will remove the first and last char from the String. Which will always be the quotes. If you need something more flexible (For instance this solution would ruin string that aren't surrounded by quotes) then you should read up on regex and java's Pattern class to see what suites your needs.

As for the solution that you provided, trim() will only remove surrounding whitespace.

Upvotes: 4

Andrew Thompson
Andrew Thompson

Reputation: 168795

import java.util.ArrayList;

class TestGenericArray {

    public static void main(String[] args) {
        String[] stringArray = {"Apple", "Banana", "Orange"};
        ArrayList<String[]> arrayList = new ArrayList<String[]>();
        arrayList.add(stringArray);
    }
}

Upvotes: 0

Related Questions