Please help me with this loop?
I have a loop:
for(int i = 0; i<array.length; i++){
array[i].trim();
if (array[i] == "" && array[i+1].contains("@")){
do{
Emails.add(array[i++]);
} while(array[i+1] != "" && !array[i+2].contains("@"));
}
It parses through a String which has multiple emails with the format of, the first line will be blank, then the next line will have an email address in it. Please look at code to see where it fails, my brain is fried. Or is there a better way to do this? Thanks!
Re: Please help me with this loop?
Don't use == to compare strings or other objects, use the equals() method. All classes have an equals() method and it is intended to do the right thing: in this case return true if and only if the two strings consist of the same characters in the same order.
Code:
// if(foo == bar) {...
if(foo.equals(bar)) {...
// if(foo != bar) {...
if(!foo.equals(bar)) {...
There is also an equalsIgnoreCase() that can be useful.
-----
When you post code, use the "code" tags. Ie put [code] at the start of the code and [/code]. That way it remains readable when it appears within a web page.
Also it is helpful if you can be descriptive. "it fails" doesn't say as much as a description of the actual behaviour when you run (or compile) the program and the behaviour you expected or wanted.
Re: Please help me with this loop?
Don't use == (or !=) with Strings. Use the equals() method instead. This advice has been repeated countless times here, so I'll let you do a search for more information.