- #1
singular
- 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:
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);
}
}