开发者

error in java programming:

开发者 https://www.devze.com 2023-01-23 14:16 出处:网络
paramList = new ArrayList<String>(); paramList.add(line.split(\",\")); When I used this, it gives me the er开发者_如何学Pythonror:
paramList = new ArrayList<String>();
paramList.add(line.split(","));

When I used this, it gives me the er开发者_如何学Pythonror:

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.


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.


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消