Simple Java Email Harvester Program

In summary, the conversation is about a programming problem with extracting email addresses using a Java program. The person is trying to find a solution using only string operations and methods. They are advised to use a regular expression instead, which they are not familiar with. The conversation also discusses the possibility of using an if-else statement to check for the presence of an email address in the input. The conversation ends with a suggestion to read a tutorial on regular expressions to better understand how to use them.
  • #1
42
0
Hey guys, I am learning Java and am attempting a project from a book, but I have run into a problem. I don't have an issue when it comes to harvesting an address that has whitespace on both sides. My issues come when a) the input doesn't contain whitespace on either side of an address and b) the input doesn't have an address at all (my program recognizes an address as containing an @ and whitespace on either side.

How do I get the program to run without error considering my two issues and simply print an "@" if there is no email address present with using only the the methods I already have in my program (I can't use looping or conditional structures)?

Here is my code thus far:

import java.util.*;

public class Harvester
{
public static void main(String[] args)
{
// Title of program
System.out.println("Email Address Harvester");

// Set up input stream
Scanner scan = new Scanner(System.in);

// Prompt input from user
System.out.print("Input line: ");

// Scan input
String input = scan.nextLine();

// Set j to index of the first "@" in input
String searchString1 = "@";
int j = input.indexOf(searchString1, 0);

// Set i to minimal index in input such that all characters in positions i through j are nonblank
String searchString2 = " ";
int i = input.lastIndexOf(searchString2, j);

// Set k to maximal index in input such that all characters in positions j through k are nonblank
int k = input.indexOf(searchString2, j);

// Set email address to the substring of input with indices i through k
String emailAddress = input.substring(i, k);

// Print email address.
System.out.println("Extracted email address: " + emailAddress);
}
}
 
Technology news on Phys.org
  • #2
Design it the right way. Use a regular expression.

- Warren
 
  • #3
chroot said:
Design it the right way. Use a regular expression.

- Warren

Ok, but if I were to stay within the bounds of using only string operations and methods, how do you suggest I fix the problem of when the input doesn't contain an email address? How would I get the program to print only an @ under this condition?
 
  • #4
Really, you should just use a regular expression. I'm not familiar with the Java classes for doing that but there's usually a .Success boolean property that simply tells you whether there was a match or not. If you want to reinvent the wheel, unless you give a good reason to you may not find lots of help to be forthcoming.
 
  • #5
singular said:
Ok, but if I were to stay within the bounds of using only string operations and methods, how do you suggest I fix the problem of when the input doesn't contain an email address? How would I get the program to print only an @ under this condition?

You're using indexOf() to find an @ sign. If there is no @ sign, indexOf() will return -1. You can check to see that the result is -1, and do something about it. As it stands, your program doesn't check it, and goes on to blindly use it in the next line of code, which then throws the exception.

- Warren
 
  • #6
chroot said:
You're using indexOf() to find an @ sign. If there is no @ sign, indexOf() will return -1. You can check to see that the result is -1, and do something about it. As it stands, your program doesn't check it, and goes on to blindly use it in the next line of code, which then throws the exception.

- Warren
Thanks for your help. I am going to forgo trying to find another solution (there doesn't seem to be one) and use an if-else statement to check the value like you suggested.
 
  • #7
Try to read this "tutorial" entirely to get a hold of how regular expressions work and how to apply them:
http://www.regular-expressions.info/tutorial.html

In fact, right in the first page they show you an example of an email pattern
Code:
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
 

1. How does the Simple Java Email Harvester Program work?

The Simple Java Email Harvester Program uses a combination of algorithms and web scraping techniques to gather email addresses from various online sources. It searches for email addresses based on specific keywords and extracts them from websites and online directories.

2. Is the Simple Java Email Harvester Program legal to use?

Yes, the Simple Java Email Harvester Program is legal to use as long as it follows ethical practices and does not violate any privacy policies or terms of use of the websites it scrapes. It is important to use the program responsibly and only for legitimate purposes.

3. Can the Simple Java Email Harvester Program be customized for specific needs?

Yes, the program can be customized to search for specific types of email addresses or to exclude certain sources. This can be done by modifying the program's code or by providing specific keywords or filters for the program to use.

4. How accurate are the results from the Simple Java Email Harvester Program?

The accuracy of the results depends on the quality of the input keywords and the sources that the program is searching through. It is important to use relevant and specific keywords to get more accurate results. Additionally, the program may occasionally extract invalid or outdated email addresses, so it is recommended to verify the results before using them.

5. Is the Simple Java Email Harvester Program easy to use?

Yes, the program is designed to be user-friendly and requires basic programming skills to run. However, some technical knowledge may be needed to customize the program or troubleshoot any issues that may arise. There are also tutorials and resources available online to help with using the program.

Suggested for: Simple Java Email Harvester Program

Replies
2
Views
1K
Replies
1
Views
618
Replies
5
Views
1K
Replies
13
Views
1K
Back
Top