Note that there are some explanatory texts on larger screens.

plurals
  1. POFind a char optimization
    text
    copied!<p>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 myname@uark.edu. 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. </p> <p>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.</p> <pre><code>public static List&lt;String&gt; parseEmail(Set&lt;String&gt; emails) { List&lt;String&gt; _names = new LinkedList&lt;String&gt;(); Iterator&lt;String&gt; 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&gt;0 &amp;&amp; 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; } </code></pre> <p>**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. </p> <p>***So there is a string.lastindexOf() and that was what I needed. </p> <pre><code>public static List&lt;String&gt; parseEmail(Set&lt;String&gt; emails) { List&lt;String&gt; _names = new LinkedList&lt;String&gt;(); Iterator&lt;String&gt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload