binibiningtinamoran

Emails.java

Nov 29th, 2019
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class Emails {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         ArrayList <String> emails = new ArrayList <>();
  9.         System.out.println("Enter emails below and type \"done\" when finished");
  10.  
  11.         String invalidMsg = " is not a valid edu email address.";
  12.         String eduDomain, email;
  13.         int dotIndex, eduIndex, atCount;
  14.  
  15.         do {
  16.             atCount = 0;
  17.             System.out.print("Enter an email: ");
  18.             email = scan.nextLine();
  19.  
  20.             if (email.length() >= 7 && email.contains("@") && email.indexOf("@") != 0) {
  21.                     for (int i = 0; i < email.length(); i++) {
  22.                         if ('@' == email.charAt(i)) {
  23.                             atCount++;
  24.                         }
  25.                     } // end for loop
  26.                
  27.                     if (atCount != 1) {
  28.                         System.out.println(email + " is not a valid edu email address.");
  29.                     } else { // atCount == 1
  30.                         eduDomain = email.substring(email.length() - 3);
  31.                         dotIndex = email.indexOf('.', email.length() - 4);
  32.                         eduIndex = email.indexOf("edu");
  33.  
  34.                         if (eduDomain.equals("edu") && dotIndex < eduIndex) {
  35.                             emails.add(email);
  36.                         } else {
  37.                             System.out.println(email + invalidMsg);
  38.                         } // end inner else
  39.                     } // end middle else
  40.             } else {
  41.                 System.out.println(email + invalidMsg);
  42.             } // end outer else
  43.         } while (!(email.equalsIgnoreCase("done")));
  44.  
  45.         System.out.println("\nHere are the valid emails: ");
  46.         for (String em : emails) {
  47.             System.out.print(em);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment