Create an interactive Java program that asks a user for some text of interest an
ID: 3552537 • Letter: C
Question
Create an interactive Java program that asks a user for some text of interest and then displays the first email address found in that text. (This is the basis for one of the programs that spammers use to mine text, such as email messages, Web pages, and blogs, to find email addresses they can then add to their lists of people to spam.) For our purposes, an email address is composed of an "@" and the two maximal-length strings with no embedded whitespace surrounding that "@". If there is no email address in the text, then the program should display just an "@".
Construct the class EmailAddress.java.
Hint: I suggest you use the 'split' method of the String class. This method will divide a given string into tokens based on any dividing character you choose. For example, if you split an input sentence based on a space " ", you will get an array of words that you can process one at a time using a loop.
Explanation / Answer
import java.util.Scanner;
public class EmailAddress {
public static void main(String args[])
{
System.out.println("Please enter the text line");
Scanner in=new Scanner(System.in);
String str=in.nextLine();
String[] str2=str.split(" ");
System.out.println("----------------------------------------");
System.out.println("Found Email id's in entered line");
for(int i=0;i<str2.length;i++)
{
if(str2[i].contains("@"))System.out.println(str2[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.