Reputation: 267
So this part of the homework wants us to take a Set of Strings and we will return a List of Strings. In the String Set we will have email addresses ie [email protected]. We are to pull the first part of the email address; the name and put it in the String List.From the above example myname would be put into the List. The code I currently have uses an iterator to pull a string from the Set. I then use the String.contains("@") as an error check to make sure the String has an @ symbol in it. I then start at the end of the string and use the string.charAt("@") to check each char. Once It's found i then make a substring with the correct part and send it to the List. My problem is i wanted to use something recursive and cut down on operations. I was thinking of something that would divide the string.length()/2 and then use String.contains("@") on the second half first. If that half does contain the @ symbol then it would call the functions recursively agin. If the back half did not contain the @ symbol then the front half would have it and we would call the function recursively sending it.
So my problem is when I call the function recursively and send it the "substring" once I find the @ symbol I will only have the index of the substring and not the index of the original string. Any ideas on how to keep track of it or maybe a command/method I should be looking at. Below is my original code. Any advice welcome.
public static List<String> parseEmail(Set<String> emails)
{
List<String> _names = new LinkedList<String>();
Iterator<String> eMailIt=emails.iterator();
while(eMailIt.hasNext())
{
String address=new String(eMailIt.next());
boolean check=true;
if(address.contains("@"))//if else will catch addresses that do not contain '@' .
{
String _address="";
for(int i=address.length(); i>0 && check; i--)
{
if('@'==address.charAt(i-1))
{
_address=new String(address.substring(0,i-1));
check=false;
}
}
_names.add(_address);
//System.out.println(_address);//fill in with correct sub string
}
else
{
//System.out.println("Invalid address");
_names.add("Invalid address");//This is whats shownn when you have an address that does not have an @ in it.
} // could have it insert some other char i.e. *%# s.t. if you use the returned list it can skip over invalid emails
}
return _names;
}
**It was suggested I use the String.indexOf("@") BUT according to the API this method only gives back the first occurrence of the symbol and I have to work on the assumption that there could be multiple "@" in the address and I have to use the last one. Thank you for the suggestion though. Am looking at the other suggestion and will report back.
***So there is a string.lastindexOf() and that was what I needed.
public static List<String> parseEmail(Set<String> emails)
{
List<String> _names = new LinkedList<String>();
Iterator<String> eMailIt=emails.iterator();
while(eMailIt.hasNext())
{
String address=new String(eMailIt.next());
if(address.contains("@"))//if else will catch addresses that do not contain '@' .
{
int endex=address.lastIndexOf('@');
_names.add(address.substring(0,endex-1));
// System.out.println(address.substring(0,endex));
}
else
{
// System.out.println("Invalid address");
_names.add("Invalid address");//This is whats shownn when you have an address that does not have an @ in it.
} // could have it insert some other char i.e. *%# s.t. if you use the returned list it can skip over invalid emails
}
return _names;
}
Upvotes: 1
Views: 107
Reputation: 80603
Don't reinvent the wheel (unless you were asked too of course). Java already has a built-in function for what you are attempting String.indexOf(String str)
. Use it.
final String email = "[email protected]";
final int atIndex = email.lastIndexOf("@");
if(atIndex != -1) {
final String name = email.substring(0, atIndex);
}
Upvotes: 2
Reputation: 23550
I agree to the previous two answers, if you are allowed to use the built-in functions split
or indexOf
then you should. However if it is part of your homework to find the substrings yourself you should definitely just go through the string's characters and stop when you found the @
aka linear search.
You should definitely not under no circumstances try to do this recursively: The idea of divide and conquer should not be abused in a situation where there is nothing to gain: Recursion means function-call overhead and doing this recursively would only have a chance of being faster than a simple linear search if the sub-strings were searched in-parallel; and even then: the synchronization overhead would kill the speedup for all but the most gigantic strings.
Upvotes: 1
Reputation: 106410
Unless recursion is specified in the homework, you would be best served by looking into String.split
. It will split the String into a String array (if you specify it to be around '@'
), and you can access both halves of the e-mail address.
Upvotes: 0