user2104612
user2104612

Reputation: 45

Parsing string from a CSV file to an integer

I have a CSV file which displays a name followed by 3 integers as shown below.

"John","12","37","1000"

I have the 3 number values indexed as values[1], values[2] and values[3].

I am trying to parse the strings from the CSV file by using the following:

int wins = Integer.parseInt(values[1]);
int losses = Integer.parseInt(values[2]);
int money = Integer.parseInt(values[3]);

My problem is that the values in the CSV file already are surrounded by " ", so when I try to parse the string it is trying to parse ""12"", ""37"" and ""1000"".

Is there any way in which I could ignore the " " in the CSV file without deleting them manually in the file?

Upvotes: 2

Views: 1979

Answers (3)

brianestey
brianestey

Reputation: 8302

You could take the substring between the quotes

values[1].substring(1, values[1].length() - 1)

Upvotes: 1

user2030471
user2030471

Reputation:

You can use String.substring(int, int) method.

Upvotes: 0

cwhsu
cwhsu

Reputation: 1523

You could use str.replace("\"","") to replace all the " in the string.

Upvotes: 2

Related Questions