D.J.
D.J.

Reputation: 13

Utilizing the Different Parts of a String when Using the split() Method JAVA

I am looping through an ArrayList of Strings called s with this while loop.

int i = 0;
while (i < s.length()) {

  i++;
  s.get(i).split(",");

}

I am trying to split() each line of s using the delimiter ",". I want to put each part of each line into a new Product object like this: new Product(s.get(i) first part, s.get(i) second part).

I can't find a way to capture and utilize each part of the string that I am splitting.

Upvotes: 0

Views: 38

Answers (2)

Anubian Noob
Anubian Noob

Reputation: 13596

The split method returns a string array.

Also, use a for loop:

for (int i=0; i<s.length(); i++) {
    String[] parts = s.get(i).split(",");
    Product product = new Product(parts[0], parts[1]);
}

Upvotes: 1

K139
K139

Reputation: 3669

String[] result = s.get(i).split(",");

result contains the individual split parts of the string.

and also in your while loop correct the length method to s.length, from s.length()

Upvotes: 2

Related Questions