//################################################################ // // Program4.java // // // Reads the URL, assuming this format: // http://www.cs.iastate.edu/~john // // and outputs an e-mail address from it // john@cs.iastate.edu // // Sample Run: // // Please enter an URL: http://www.iastate.edu/~john // // =====Debugging Information===== // // You entered: [http://www.cs.iastate.edu/~john] // The '.' is at index = 10 // The '~' is at index = 26 // Domain name = [cs.iastate.edu] // User name = [john] // // =====End of Debugging Information===== // // // The e-mail address is: john@cs.iastate.edu // //################################################################ import java.util.Scanner; public class Program4 { public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Please enter an URL: "); String url= scan.nextLine(); // Use this line if you don't want to type it all the time //String url="http://www.cs.iastate.edu/~john"; System.out.println("\n=====Debugging Information=====\n"); System.out.println("You entered: [" + url + "]"); // find the index of the first '.' int periodIdx = url.indexOf('.'); System.out.println("The '.' is at index = " + periodIdx); // find the index of the '~' int tildaIdx = url.indexOf('~'); System.out.println("The '~' is at index = " + tildaIdx); // Extract the doamin name from the URL String domainName= url.substring(periodIdx+1, tildaIdx-1); // -1 so the '/' is not included System.out.println("Domain name = [" + domainName + "]"); // Extract the user name from the URL String userName= url.substring(tildaIdx+1, url.length()); System.out.println("User name = [" + userName + "]"); System.out.println("\n=====End of Debugging Information=====\n"); // Print the e-mail address in this format: // userName@domainName String emailAddress = userName + "@" + domainName; System.out.println("\nThe e-mail address is: " + emailAddress); } }