Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Emails {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- ArrayList <String> emails = new ArrayList <>();
- System.out.println("Enter emails below and type \"done\" when finished");
- String invalidMsg = " is not a valid edu email address.";
- String eduDomain, email;
- int dotIndex, eduIndex, atCount;
- do {
- atCount = 0;
- System.out.print("Enter an email: ");
- email = scan.nextLine();
- if (email.length() >= 7 && email.contains("@") && email.indexOf("@") != 0) {
- for (int i = 0; i < email.length(); i++) {
- if ('@' == email.charAt(i)) {
- atCount++;
- }
- } // end for loop
- if (atCount != 1) {
- System.out.println(email + " is not a valid edu email address.");
- } else { // atCount == 1
- eduDomain = email.substring(email.length() - 3);
- dotIndex = email.indexOf('.', email.length() - 4);
- eduIndex = email.indexOf("edu");
- if (eduDomain.equals("edu") && dotIndex < eduIndex) {
- emails.add(email);
- } else {
- System.out.println(email + invalidMsg);
- } // end inner else
- } // end middle else
- } else {
- System.out.println(email + invalidMsg);
- } // end outer else
- } while (!(email.equalsIgnoreCase("done")));
- System.out.println("\nHere are the valid emails: ");
- for (String em : emails) {
- System.out.print(em);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment