-1

I have a string that reads like this

"Anderson, T",CWS,SS,123,498,81,167,32,0,18,56,15,109,17,5,0.335,0.357,0.508,0.865

I need to extract each part. Ive already parsed the fist three string ("Anderson, T", CWS, SS) but now i need to extract 123 and im having some trouble doing it. The way I did it for the first three fields is i just used line.split

public static String parsePOS(String line) {
    String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
    return tokens[2];
}

But I cannot use line.split with an int array. Can anyone help me out?

Thanks!

1
  • There is nothing different between SS and 123, they are both just a sequence of characters in a string. If you want to get the 123 value as an int value, first get the text from tokens[3], not tokens[2], then convert the string value to an int value by calling Integer.parseInt(s) Commented Nov 22, 2019 at 21:51

1 Answer 1

1

You can do it as follows:

public class Main {
    public static void main(String[] args) {
        String str="\"Anderson, T\",CWS,SS,123,498,81,167,32,0,18,56,15,109,17,5,0.335,0.357,0.508,0.865";
        int x=Integer.parseInt(parsePOS(str));
        System.out.println(x);//123
        System.out.println(x+1);//124
    }
    public static String parsePOS(String line) {
        String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
        //System.out.println(Arrays.toString(tokens));
        return tokens[3];
    }
}

Use Integer.parseInt(String) to parse an integer in String format.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I really appreciate it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.