0

the variable TEST is equal to this

[email protected]_Hd_s [email protected]_Update_on the [email protected]_Ksks_ajsj 

i want to pull each "product" out so i have an ArrayList equal to this

[email protected]_Hd_s
[email protected]_Update_on
[email protected]_Ksks_ajsj

Right now the only thing in my array list is [email protected]_Hd_s

How can i pull each "product" from the one variable (TEST) in a loop and add it to the ArrayList?

My code so far:

String TEST = result;

ArrayList<String> Products = new ArrayList<>();
boolean flag = true;

    while(flag == true){
      Products.add(TEST.substring(0, TEST.indexOf(' ')));
      TEST = TEST.substring(TEST.indexOf(' ') + 1);

          if(TEST.equals("")){
               flag = false;
          }else{
               TEST = TEST.substring(1);
           }
    }

see

3 Answers 3

1

Your one step away from doing it. After the first iteration of your while loop, you do retrieve [email protected]_Hd_s, but after that the loop runs infinitely because the other parts of the string are not being accessed. The solution is to cut out the part you retrieved from the string each time you add it to Products. I should also note that this will only work if TEST ends with a space " ". Here is a way to approach this.

String TEST = result;

ArrayList<String> Products = new ArrayList<>();
boolean flag = true;

while(flag == true){
  Products.add(TEST.substring(0,TEST.indexOf(' ')));
  TEST = TEST.substring(TEST.indexOf(' '));//cutting the last email added from the string
    if(TEST.equals(" ")){
       flag = false;
    }
    else{
       TEST = TEST.substring(1); //remove that space so that it doesn't get  
                                 //counted again in the next iteration
    }

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

12 Comments

i think because of the " " at the end of TEST its not working
Would you prefer it to work without the " " at the end?
oh never mind it works! but for reference later could you take away the " " at the end?
also the second time it runs the loop the "product" losses the "l" at the beginning!
what "I"? also, you can't take out the " " with this code because you need the " " to separate each email from each other. To make it work without the " " at the end, you would have to check TEST to see if there are any spaces left to signify the last email being added.
|
0

Seeing your input string doesn't simply have email separated by white space, I suggest you use Pattern and Matcher. First you need to define the email's pattern (you can google it), then use the example in this : http://www.tutorialspoint.com/java/java_regular_expressions.htm

Comments

0

An alternative one line solution using String.split() function:

List<String> products = Arrays.asList(TEST.split(" "));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.