2

I have a question regarding indexOf(). I am trying to program an EmailExtractor (Yes, this is a homework but I am not looking for code) which extracts the entire email address from a sentence that is input by a user.

For example - User Input: Mail us at abc@def.ghi.jk with your queries. The program will then display abc@def.ghi.jk from the above String. I understand indexOf() and substring() are required.

The idea I have now is to use indexOf() to locate the '@', and then search for the empty space just before the email address input by the user (nth).

My code is as follows:

System.out.println("This is an Email Address Extractor.\n");
System.out.print("Enter a line of text with email address: ");
String emailInput = scn.nextLine();
int spaceAt = emailInput.indexOf(" ");
for (int i = 1; i <= emailInput.indexOf("@"); i++){
    if (spaceAt < emailInput.indexOf("@")) {
        spaceAt = emailInput.indexOf(" ", spaceAt + 1);
    }
}

I understand and am aware of the problem in my code.

1) "Mail us at abc@def.ghi.jk with your queries".indexOf(" ") is 4, I am trying to get 10. However, the IF Condition I have input will cause it to skip to the next instance of indexOf() which is 25. (Because 10 < 14).

How do I go about avoiding this from happening?

Once again, I am not looking for purely the answer rather, I am trying to work around a solution. Thanks in advance!

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Kayjaeemm
  • 31
  • 2
  • 3
  • 1
    Is it necessary to use `indexOf` method? – Rohit Jain Sep 20 '13 at 16:02
  • Why don't you split the String and then find which one has an `@` within ? – Konstantin Yovkov Sep 20 '13 at 16:09
  • The simple solution is to use some other variable instead of `spaceAt` to hold the result of `emailInput.indexOf(" ",...)`, and make sure the result is less than the index of `@`. Then assign `spaceAt` to that result *only* if it's less. Otherwise, you can use `break` to get out of the `for` loop since you're done looking. This is an example of "lookahead", i.e. you have to look at the next result to see if it's acceptable before you actually use it. Also think about what will happen if there are no more spaces when you do your `indexOf`. – ajb Sep 20 '13 at 16:12
  • +1 for stating clearly it is an homework, that you don't want code, and for showing what you tried. – Andrea Ligios Sep 20 '13 at 16:13
  • Not every word with @ is e-mail address. Why don't you use Regex? – tomwesolowski Sep 20 '13 at 16:13
  • @Rohit : Yes, as mentioned this is a homework. We were taught up till String Operations, therefore the best method for me would be indexOf() and substring(). Thanks! – Kayjaeemm Sep 20 '13 at 17:43
  • @ajb: Thank you so much for the input! I will work along those lines. I have tried working with break; but with your explanations, I understand it much better now. Thanks! – Kayjaeemm Sep 20 '13 at 17:44

3 Answers3

3

How about finding the space before and after the @

int at = emailInput.indexOf('@');
int start = emailInput.lastIndexOf(' ', at) + 1;
int end = emailInput.indexOf(' ', at);
if (end == -1) end = emailInput.length();
String email = emailInput.substring(start, end);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

you can use regular expressions instead of using indexOf() and substring()

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"

use the Pattern and Matcher to validate the email id in your string. This may give you a clear view on it

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
0

One thing that you could do is call indexOf and test the result in the same iteration of the loop, instead of saving the check for the next iteration. Something like this in mostly pseudocode:

spaceAt := 0
indexOfAtSign := emailInput.indexOf('@');
while (spaceAt < indexOfAtSign) { // you never really used i anyway
    temp := index of next space
    if (temp > indexOfAtSign) break;
    else spaceAt = temp;
}

(There'd need to be a small bit of corner-case testing, but only for if there are no spaces either before the @ sign or after the @ sign)

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36