コード例 #1
0
  public void init() {
    Scanner scan = new Scanner(System.in);
    n = scan.nextInt();
    m = scan.nextInt();
    for (int i = 0; i < n; i++) {
      String input = scan.next();
      record.add(input);
    }

    for (int i = 0; i < m; i++) {
      String input = scan.next();
      char[] charArray = input.toCharArray();
      boolean isValid = false;
      for (int j = 0; j < charArray.length; j++) {
        char c = charArray[j];
        for (char d = 'a'; d <= 'c'; d = (char) (d + 1)) {
          if (d == c) continue;
          charArray[j] = d;
          if (record.contains(new String(charArray))) {
            isValid = true;
            break;
          }
        }
        charArray[j] = c;
        if (isValid) break;
      }
      if (isValid) System.out.println("YES");
      else System.out.println("NO");
    }
    scan.close();
  }
コード例 #2
0
 public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   int n = inp.nextInt();
   String[] store = new String[n];
   for (int i = 0; i < n; i++) store[i] = inp.next();
   int[] cnt = new int[n];
   Arrays.fill(cnt, 0);
   String str = inp.next();
   for (int j = 0; j < n; j++) {
     int l1 = store[j].length();
     for (int k = 0; k <= (str.length() - l1); k++) {
       if (str.substring(k, k + l1).equals(store[j])) {
         cnt[j] = cnt[j] + 1;
       }
     }
   }
   int y = 0;
   for (int m = 0; m < n; m++) {
     y = Math.max(y, cnt[m]);
   }
   System.out.println(y);
   for (int h = 0; h < n; h++) {
     if (cnt[h] == y) System.out.println(store[h]);
   }
 }
コード例 #3
0
 public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   BigInteger a = new BigInteger(s.next());
   BigInteger b = new BigInteger(s.next());
   BigInteger ad = a.add(b);
   BigInteger mu = a.multiply(b);
   System.out.println(ad);
   System.out.println(mu);
 }
コード例 #4
0
ファイル: uva706.java プロジェクト: de-yu/uva
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    int num[][] = {
      {0, 2, 3, 5, 6, 7, 8, 9},
      {2, 3, 4, 5, 6, 8, 9},
      {0, 2, 3, 5, 6, 8, 9},
      {0, 4, 5, 6, 8, 9},
      {0, 1, 2, 3, 4, 7, 8, 9},
      {0, 2, 6, 8},
      {0, 1, 3, 4, 5, 6, 7, 8, 9}
    };

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      String b = scanner.next();
      if (a == 0) break;
      String data[][] = new String[5 + (a - 1) * 2][(3 + (a - 1)) * b.length()];

      for (int i = 0; i < 5 + (a - 1) * 2; i++)
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) data[i][j] = " ";

      for (int i = 0; i < b.length(); i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[(a + 1) * j][1 + i * (a + 2) + h] = "-";
            }
          }
        }
        for (int j = 3; j < 5; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[1 + h][(j - 3) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
        for (int j = 5; j < 7; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[a + 2 + h][(j - 5) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
      }
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < 5 + (a - 1) * 2; i++) {
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) {
          if ((j - (a + 1)) % (a + 2) == 0 && (j + 1) != (3 + (a - 1)) * b.length())
            sb.append(data[i][j] + " ");
          else sb.append(data[i][j]);
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
コード例 #5
0
ファイル: pathToPrincess.java プロジェクト: spl0i7/HackerRank
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int m;
   m = in.nextInt();
   String grid[] = new String[m];
   for (int i = 0; i < m; i++) {
     grid[i] = in.next();
   }
   displayPathtoPrincess(m, grid);
 }
コード例 #6
0
ファイル: uva619.java プロジェクト: de-yu/uva
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    while (scanner.hasNext()) {
      String str = scanner.next();
      if (str.equals("*")) break;
      if ((int) str.charAt(0) < 97) num(str);
      else abcd(str);
    }
  }
コード例 #7
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int R = in.nextInt();
     int C = in.nextInt();
     String G[] = new String[R];
     for (int G_i = 0; G_i < R; G_i++) {
       G[G_i] = in.next();
     }
     int r = in.nextInt();
     int c = in.nextInt();
     String P[] = new String[r];
     for (int P_i = 0; P_i < r; P_i++) {
       P[P_i] = in.next();
     }
     // Check Grid
     checkGrid(G, P);
   }
 }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String time = in.next();

    // Set format for input and output
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ssa");
    SimpleDateFormat out = new SimpleDateFormat("HH:mm:ss");

    try {
      Date inTime = sdf.parse(time);
      System.out.println(out.format(inTime));
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
コード例 #9
0
ファイル: uva10905.java プロジェクト: de-yu/uva
  public static void main(String[] args) throws IOException {

    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int num;
    while ((num = scanner.nextInt()) != 0) {
      ArrayList<String> all = new ArrayList<String>();
      for (int i = 0; i < num; i++) {
        all.add(scanner.next());
      }
      Collections.sort(all, comptr);
      StringBuilder sb = new StringBuilder("");
      for (String s : all) {
        sb.append(s);
      }
      System.out.println(sb);
    }
  }
コード例 #10
0
ファイル: ACMICPCTeam.java プロジェクト: narren/naren
 public static void main(String[] args) throws NumberFormatException, IOException {
   Scanner sc = new Scanner(System.in);
   int N = sc.nextInt();
   int M = sc.nextInt();
   int maxValue = (int) Math.pow(2, 5) - 1;
   int[] array = new int[N];
   int first = 0;
   int second = 0;
   for (int i = 0; i < N; i++) {
     array[i] = Integer.parseInt(sc.next(), 2);
   }
   for (int i = 0; i < array.length; i++) {
     for (int j = i + 1; j < array.length; j++) {
       int or = (array[i] | array[j]);
       if (or > first) {
         first = or;
       }
     }
   }
   System.out.println(first + " " + second);
 }
コード例 #11
0
ファイル: CommonChild.java プロジェクト: DdMad/OJSandBox
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    String s2 = sc.next();

    int[][] f = new int[s1.length()][s2.length()];

    for (int i = 0; i < s1.length(); i++) {
      for (int j = 0; j < s2.length(); j++) {
        f[i][j] = 0;
      }
    }

    for (int i = 0; i < s1.length(); i++) {
      for (int j = 0; j < s2.length(); j++) {
        // important!
        if (i == 0 || j == 0) {
          if (s1.charAt(i) == s2.charAt(j)) {
            f[i][j] = 1;
          } else {
            if (i > 0) {
              f[i][j] = f[i - 1][j];
            }
            if (j > 0) {
              f[i][j] = f[i][j - 1];
            }
          }
        } else {
          if (s1.charAt(i) == s2.charAt(j)) {
            f[i][j] = getMax(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1] + 1);
          } else {
            f[i][j] = getMax(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]);
          }
        }
      }
    }

    System.out.println(f[s1.length() - 1][s2.length() - 1]);
  }
コード例 #12
0
ファイル: Solution.java プロジェクト: mlin6436/dojo4j
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();

    int l = s.length();
    Double sqrl = Math.sqrt(l);
    Double low = Math.floor(sqrl);
    Double high = Math.ceil(sqrl);
    int r = 0;
    int c = 0;

    if (low == high) {
      r = low.intValue();
      c = low.intValue();
    } else if (low * high >= l) {
      r = low.intValue();
      c = high.intValue();
    } else {
      r = high.intValue();
      c = high.intValue();
    }

    //        System.out.println("Row: " + r);
    //        System.out.println("Col: " + c);

    StringBuilder sb = new StringBuilder();
    char en[] = s.toCharArray();
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        if (j * c + i < l) {
          sb.append(en[j * c + i]);
        }
      }
      sb.append(" ");
    }

    System.out.println(sb.toString());
  }
コード例 #13
0
ファイル: C.java プロジェクト: jmankhan/ACM
  public void solve() throws Exception {
    n = in.nextInt();
    m = in.nextInt();

    graph = new char[n][m];
    cracked = new boolean[n][m];

    for (int i = 0; i < n; i++) {
      graph[i] = in.next().trim().toCharArray();
    }
    sx = in.nextInt() - 1;
    sy = in.nextInt() - 1;

    gx = in.nextInt() - 1;
    gy = in.nextInt() - 1;

    cracked[sx][sy] = true;

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {}
    }

    int scount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        scount++;
      }
    }

    int gcount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = gx + dx[i];
      int ny = gy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        gcount++;
      }
    }

    boolean isNeighbors = false;

    for (int i = 0; i < 4 && !isNeighbors; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }
      if (nx == gx && ny == gy) {
        isNeighbors = true;
      }
    }

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {
        if (scount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      } else {
        if (scount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (isNeighbors) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (dfs(sx, sy)) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else {
      out.println("NO");
    }
  }
コード例 #14
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t != 0) {
      int R, C, r, c;
      R = sc.nextInt();
      C = sc.nextInt();
      char grid[][] = new char[R][C];

      for (int i = 0; i < R; i++) {
        String l = sc.next();
        for (int j = 0; j < C; j++) {
          grid[i][j] = l.charAt(j);
        }
      }

      r = sc.nextInt();
      c = sc.nextInt();
      char pattern[][] = new char[r][c];
      for (int i = 0; i < r; i++) {
        String l = sc.next();
        for (int j = 0; j < c; j++) {
          pattern[i][j] = l.charAt(j);
        }
      }
      int i = 0;
      String g = new String(grid[i]);
      String p = new String(pattern[i]);
      int present = 0;
      while (i < R) {
        int cc = g.indexOf(p);
        int flag = 0;
        if (cc != -1) {
          for (int k = 1; k < r; k++) {
            g = new String(grid[i + k]);
            p = new String(pattern[k]);
            int x = g.indexOf(p, cc);
            if (x != cc) {
              flag = 1;
              break;
            }
          }
          if (flag == 1) {
            p = new String(pattern[0]);
          } else {
            System.out.println("YES");
            present = 1;
            break;
          }
        }

        i++;
        if (i > R - r) {
          break;
        }
        g = new String(grid[i]);
      }
      if (present == 0) {
        System.out.println("NO");
      }

      t--;
    }
  }
コード例 #15
0
  public static void contactBookOperations(String contactBookName)
      throws IOException, ParseException {
    FileWriter fr = new FileWriter(contactBookName, true);
    BufferedReader fileReader = new BufferedReader(new FileReader(contactBookName));
    BufferedWriter fileWriter = new BufferedWriter(fr);
    String address = "";
    String name = "";
    boolean validName = false;
    String dateOfBirth = "";
    boolean dateValid = false;
    String petName = "";
    int tag = 0;
    String contactType = "";
    boolean invalidTag = false;
    boolean moreEmail = false;
    String email = "";
    String emailChoice = "";
    boolean morePhone = true;
    String phoneChoice = "";
    String phoneList = "";
    String contactAdded = "";
    long phoneNum = 0;
    int choice = 0;
    ArrayList<String> nameList =
        new ArrayList<String>(); // arrayList to hold names of contacts in the file...
    Scanner sc = new Scanner(System.in);
    String searchString = "";
    Scanner stringReader = new Scanner(System.in);
    String line = "";
    String totalDetails = "";
    // System.out.println(fileReader);
    while (choice != 6) {
      System.out.println("\nTO ADD CONTACT->PRESS 1 ");
      System.out.println("TO EDIT CONTACT->PRESS 2 ");
      System.out.println("TO REMOVE CONTACT->PRESS 3 ");
      System.out.println("TO LIST CONTACTS->PRESS 4 ");
      System.out.println("TO SEARCH CONTACT->PRESS 5 ");
      System.out.println("TO GO BACK TO PREVIOUS MENU->PRESS 6 ");
      while (!sc.hasNextInt()) {
        System.out.println("\nENTER ONLY NUMBERS RANGED FROM 0 - 6");
        sc.next();
      }

      choice = sc.nextInt();
      switch (choice) {
          // ADD A NEW CONTACT........
        case 1: // add contact option.............
          // add name
          dateValid = false;
          invalidTag = true;
          moreEmail = true;
          morePhone = true;
          email = "";
          phoneList = "";
          nameList.clear();
          System.out.println("\nENTER NAME OF THE PERSON(Ex: Aditya Dey)");
          validName = false;
          fileReader = new BufferedReader(new FileReader(contactBookName));
          while (!validName) {
            name = stringReader.nextLine(); // read contact name from user
            name = name.trim();
            if (name.equals("")) {
              System.out.println(
                  "\nNAME OF THE PERSON CANNOT BE BLANK\nENTER NAME OF THE PERSON(Ex: Aditya Dey)");
              continue;
            }
            while (((line = fileReader.readLine()) != null)) // read till end of file
            {
              String s =
                  line.substring(0, line.indexOf('=')); // add each name that exists in the file
              if (!nameList.contains(s)) {
                nameList.add(s);
              }
            }
            // System.out.println(nameList);
            if (nameList.contains(name)) // if name already exists, print error message
            {
              System.out.println("\nA CONTACT NAMED " + name + " ALREADY EXISTS.");
              System.out.println("\nPLEASE ADD ANOTHER NAME(Ex: Aditya Dey)");

            } else {
              validName = true;
              nameList.add(name);
            }
          }
          // date of birth
          System.out.println(
              "\nENTER DOB OF THE PERSON IN DD/MM/YYYY FORMAT Ex: 12/05/1992 OR PRESS ENTER TO SKIP");
          while (!dateValid) {
            dateOfBirth = stringReader.nextLine();
            if (dateOfBirth.trim().compareTo("") == 0) dateValid = true;
            else // doubt? next() skips next scan...
            dateValid = isValidDate(dateOfBirth);
            if (!dateValid) {
              System.out.println(
                  "\nINVALID DATE...\nENTER DOB OF THE PERSON IN DD/MM/YYYY FORMAT Ex: 12/05/1992");
            }
          }

          //	System.out.println(dateOfBirth);
          // add contact type
          System.out.println("\nENTER PET NAME OF THE PERSON OR ENTER TO SKIP");
          petName = stringReader.nextLine();

          System.out.println("\nCHOOSE CONTACT TYPE...");
          System.out.println("\nFOR FAMILY->PRESS 1 ");
          System.out.println("FOR FRIENDS->PRESS 2 ");
          System.out.println("FOR ASSOCIATES->PRESS 3 ");
          System.out.println("FOR OTHERS->PRESS 4 ");

          while (!sc.hasNextInt()) {
            System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1 - 4");
            sc.nextLine();
          }
          tag = sc.nextInt();

          while (invalidTag) {
            switch (tag) {
              case 1:
                contactType = "Family";
                invalidTag = false;
                break;
              case 2:
                contactType = "Friend";
                invalidTag = false;
                break;
              case 3:
                contactType = "Associate";
                invalidTag = false;
                break;
              case 4:
                contactType = "Others";
                invalidTag = false;
                break;
              default:
                System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1-4");
                invalidTag = true;
                break;
            }
          }
          // System.out.println(contactType);
          System.out.println("\nENTER ADDRESS IN ONE LINE OR PRESS ENTER TO SKIP");
          address = stringReader.nextLine();
          // System.out.println(address);
          while (moreEmail) {
            System.out.println("\nENTER EMAIL ADDRESS OR PRESS ENTER TO SKIP");
            email = email + "," + stringReader.nextLine();
            // System.out.println(email);
            if (!(email.trim()).equals(",")) {
              System.out.println(
                  "\nTO ADD EMAIL ADRESS->PRESS 'Y' OR 'y'..TO STOP ADDING EMAIL PRESS ANY OTHER KEY");
              emailChoice = stringReader.nextLine();
              if (emailChoice.equals("y") || emailChoice.equals("Y")) {
                moreEmail = true;
              } else {
                moreEmail = false;
              }
            } else {
              moreEmail = false;
            }
          }
          email = email.substring(1, email.length());
          // System.out.println(email);

          while (morePhone) {
            System.out.println("\nENTER CONTACT NUMBER");

            while (!sc.hasNextLong()) {
              System.out.println("\nINVALID CONTACT NUMBER.\nENTER A VALID CONTACT NUMBER");
              sc.next();
            }
            phoneNum = sc.nextLong();
            phoneList = phoneList + "," + phoneNum;
            sc.nextLine(); // doubt? not included: doesnt read phonechoice.....

            System.out.println(
                "\nTO ADD MORE PHONE NUMBERS->PRESS 'Y' OR 'y' \nOTHERWISE PRESS ANY OTHER KEY");
            phoneChoice = sc.nextLine();

            if (phoneChoice.equals("y") || phoneChoice.equals("Y")) {
              morePhone = true;
            } else {
              morePhone = false;
            }
          }
          phoneList = phoneList.substring(1, phoneList.length());
          System.out.println("FOLLOWING DETAILS HAS BEEN SAVED IN THE PHONEBOOK");
          System.out.println("---------------------------------------------------");
          System.out.println(
              "["
                  + name
                  + ","
                  + petName
                  + ","
                  + contactType
                  + ","
                  + address
                  + ","
                  + dateOfBirth
                  + ","
                  + email
                  + ","
                  + phoneList
                  + "]");
          totalDetails =
              name
                  + "="
                  + dateOfBirth
                  + ":"
                  + petName
                  + ":"
                  + contactType
                  + ":"
                  + address
                  + ":"
                  + email
                  + ":"
                  + phoneList
                  + ":"
                  + new Date();
          fileWriter.write(totalDetails);
          fileWriter.newLine();
          fileWriter.flush();
          break;
          // EDITING A CONTACT
        case 2:
          String editName = "";
          String details = "";
          String line3 = "";
          String[] detailParts;
          String newDob = "";
          String newPetName = "";
          String newAddress = "";
          String newContactType = "";
          String newEmail = "";
          String newPhone = "";
          String newString = "";
          String name3 = "";
          choice = 0;
          nameList.clear();
          String[] parts;
          fileReader = new BufferedReader(new FileReader(contactBookName));
          while (((line = fileReader.readLine()) != null)) // read till end of file
          {
            String s =
                line.substring(0, line.indexOf('=')); // add each name that exists in the file
            if (!nameList.contains(s)) {
              nameList.add(s);
            }
          }
          BufferedReader editReader = new BufferedReader(new FileReader(contactBookName));
          dateValid = false;
          invalidTag = true;
          String editContact = "";
          System.out.println("\nENTER THE CONTACT TO BE EDITED");
          editName = stringReader.nextLine();
          if (!nameList.contains(editName)) {
            System.out.println("\nA CONTACT NAMED " + editName + " DOES NOT EXIST");
          } else {
            while ((line3 = editReader.readLine()) != null) {
              // System.out.println(line3);
              name3 = line3.substring(0, line3.indexOf("="));
              if (!name3.equals(editName)) {
                newString = newString + line3 + System.getProperty("line.separator");
              } else {
                editContact = line3;
              }
            }
            details = editContact.substring(editContact.indexOf("=") + 1, editContact.length());
            detailParts = details.split(":");
            newDob = detailParts[0];
            newPetName = detailParts[1];
            newContactType = detailParts[2];
            newAddress = detailParts[3];
            newEmail = detailParts[4];
            newPhone = detailParts[5];
            while (choice != 7) {
              System.out.println("\nTO EDIT DATE OF BIRTH->PRESS 1");
              System.out.println("TO EDIT PET NAME->PRESS 2 ");
              System.out.println("TO EDIT CONTACT TYPE->PRESS 3 ");
              System.out.println("TO EDIT ADDRESS->PRESS 4 ");
              System.out.println("TO ADD EMAIL->PRESS 5 ");
              System.out.println("TO ADD PHONE NUMBER->PRESS 6 ");
              System.out.println("TO GO BACK TO THE PREVIOUS MENU->PRESS 7 ");
              System.out.print("ENTER YOUR CHOICE::\t");
              while (!sc.hasNextInt()) {
                System.out.println("ENTER ONLY INTEGERS IN THE RANGE 1-7");
                sc.nextLine();
              }

              choice = sc.nextInt();
              switch (choice) {
                case 1:
                  System.out.println(
                      "\nENTER DOB OF THE PERSON IN DD/MM/YYYY FORMAT EG: 12/05/1992");
                  while (!dateValid) {
                    newDob = stringReader.nextLine(); // doubt? next() skips next scan...		
                    dateValid = isValidDate(newDob);
                    if (!dateValid) {
                      System.out.println(
                          "\nINVALID DATE...ENTER DOB OF THE PERSON IN DD/MM/YYYY FORMAT EG: 12/05/1992");
                    }
                  }
                  break;
                case 2:
                  System.out.println("\nENTER NEW PET NAME OF THE PERSON OR ENTER TO SKIP");
                  newPetName = stringReader.nextLine();
                  break;

                case 3:
                  System.out.println("\nCHOOSE CONTACT TYPE...");
                  System.out.println("\nFOR FAMILY->PRESS 1 ");
                  System.out.println("FOR FRIENDS->PRESS 2 ");
                  System.out.println("FOR ASSOCIATES->PRESS 3 ");
                  System.out.println("FOR OTHERS->PRESS 4 ");
                  while (!sc.hasNextInt()) {
                    System.out.println("\nENTER ONLY NUMBERS IN THE RANGE 1 - 3");
                    sc.nextLine();
                  }
                  tag = sc.nextInt();
                  while (invalidTag) {
                    switch (tag) {
                      case 1:
                        newContactType = "Family";
                        invalidTag = false;
                        break;

                      case 2:
                        newContactType = "Friend";
                        invalidTag = false;
                        break;

                      case 3:
                        newContactType = "Associate";
                        invalidTag = false;
                        break;

                      case 4:
                        newContactType = "Others";
                        invalidTag = false;
                        break;

                      default:
                        System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1-4");
                        invalidTag = true;
                        break;
                    }
                  }
                  break;
                case 4:
                  System.out.println("\nENTER NEW ADDRESS IN ONE LINE");
                  newAddress = stringReader.nextLine();
                  break;

                case 5:
                  moreEmail = true;
                  while (moreEmail) {
                    System.out.println("\nENTER EMAIL ADDRESS OR PRESS ENTER TO SKIP");
                    newEmail = newEmail + "," + stringReader.nextLine();
                    // System.out.println(email);
                    if (!(newEmail.trim()).equals(",")) {
                      System.out.println(
                          "TO ADD EMAIL->PRESS 'Y' OR 'y'..TO STOP ADDING EMAIL PRESS ANY OTHER KEY");
                      emailChoice = stringReader.nextLine();
                      if (emailChoice.equals("y") || emailChoice.equals("Y")) {
                        moreEmail = true;
                      } else {
                        moreEmail = false;
                      }
                    } else {
                      moreEmail = false;
                    }
                  }
                  break;

                case 6:
                  morePhone = true;
                  while (morePhone) {
                    System.out.println("\nENTER PHONE NUMBER");
                    while (!sc.hasNextLong()) {
                      System.out.println("\nONLY VALID PHONE NUMBERS ALLOWED");
                      sc.next();
                    }
                    phoneNum = sc.nextLong();
                    newPhone = phoneNum + ",";
                    sc.nextLine(); // doubt? not included: doesnt read phonechoice.....
                    System.out.println(
                        "\nTO ADD MORE PHONE NUMBERS->PRESS 'y' or 'Y'\nOTHERWISE PRESS ANY OTHER KEY");
                    phoneChoice = sc.nextLine();
                    if (phoneChoice.equals("y") || phoneChoice.equals("Y")) {
                      morePhone = true;
                    } else {
                      morePhone = false;
                    }
                  }
                  phoneList = newPhone;
                  phoneList = phoneList.substring(1, phoneList.length());
                  break;

                case 7:
                  System.out.println(
                      "["
                          + editName
                          + ","
                          + newPetName
                          + ","
                          + newContactType
                          + ","
                          + newAddress
                          + ","
                          + newDob
                          + ","
                          + newEmail
                          + ","
                          + newPhone
                          + "]");
                  String newDetails =
                      editName
                          + "="
                          + newDob
                          + ":"
                          + newPetName
                          + ":"
                          + newContactType
                          + ":"
                          + newAddress
                          + ":"
                          + newEmail
                          + ":"
                          + newPhone
                          + ":"
                          + new Date();
                  newString = newString + newDetails + System.getProperty("line.separator");
                  BufferedWriter editWriter = new BufferedWriter(new FileWriter(contactBookName));
                  editWriter.write(newString);
                  editWriter.close();
                  break;

                default:
                  System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1-7");
                  break;
              }
            }
          }
          // dob:petname:tag:address:email1,email2,email3..:ph1,ph2,...:crtdate
          break;

        case 3: // REMOVING A CONTACT
          BufferedReader removalReader = new BufferedReader(new FileReader(contactBookName));
          String name2 = "";
          String line2 = "";
          String nameRemoved = "";
          String finalString = "";
          System.out.println("\nENTER THE NAME TO BE REMOVED");
          nameRemoved = stringReader.nextLine();
          if (!nameList.contains(nameRemoved)) {
            System.out.println("\nA CONTACT WITH NAME " + nameRemoved + " DOES NOT EXIST\n");
          } else {
            while ((line2 = removalReader.readLine()) != null) {
              name2 = line2.substring(0, line2.indexOf("="));
              if (!name2.equals(nameRemoved)) {
                finalString = finalString + line2 + System.getProperty("line.separator");
              }
            }
            BufferedWriter removalWriter = new BufferedWriter(new FileWriter(contactBookName));
            removalWriter.write(finalString);
            removalWriter.close();
            System.out.println("CONTACT " + nameRemoved + " REMOVED ");
          }
          break;
          // LISTING ELEMENTS
        case 4:
          int listChoice = 0;
          BufferedReader listReader = new BufferedReader(new FileReader(contactBookName));
          parts = null;
          TreeMap<String, String> nameDetailMap = new TreeMap<String, String>();
          String line4 = "";

          while (listChoice != 4) {
            System.out.println("\nTO DISPLAY CONTACTS BY ALPHABETICAL ORDERING OF NAMES->PRESS 1 ");
            System.out.println("TO DISPLAY CONTACTS BY CREATED DATE->PRESS 2 ");
            System.out.println("TO DISPLAY CONTACTS BY TAG->PRESS 3 ");
            System.out.println("TO GO BACK TO PREVIOUS MENU->PRESS 4 ");
            System.out.print("ENTER CHOICE::\t");

            while (!sc.hasNextInt()) {
              System.out.println("ENTER ONLY INTEGERS IN THE RANGE 1-4");
              sc.next();
            }

            listChoice = sc.nextInt();

            switch (listChoice) {
                // ORDERING CONTACTS BY ALPHABETICAL ORDERING OF NAMES..............
              case 1:
                listReader = new BufferedReader(new FileReader(contactBookName));

                line4 = "";
                parts = null;
                while ((line4 = listReader.readLine()) != null) {
                  parts = line4.split("=");
                  nameDetailMap.put(parts[0], parts[1]);
                }

                Collection<String> c = nameDetailMap.keySet();
                Iterator<String> it = c.iterator();
                if (it.hasNext()) {
                  System.out.println(
                      "\nCONTACTS BELOW ARE LISTED IN ALPHABETICAL ORDERING OF NAMES");
                  System.out.println("-----------------------------------------------------------");
                } else {
                  System.out.println("NO CONTACTS AVAILABLE");
                }
                while (it.hasNext()) {
                  String s = (String) it.next();
                  System.out.println("\n\n" + s);
                  System.out.println("---------------------------------");
                  String[] contactDetail = nameDetailMap.get(s).split(":");
                  for (int i = 0; i < contactDetail.length; i++) {
                    if (contactDetail[i].trim().compareTo("") == 0) {
                      contactDetail[i] = "Unavailable";
                    }
                  }
                  System.out.println("Date of Birth::\t" + contactDetail[0]);
                  System.out.println("Pet name::\t" + contactDetail[1]);
                  System.out.println("Contact Type::\t" + contactDetail[2]);
                  System.out.println("Address::\t" + contactDetail[3]);
                  System.out.println("Email Address::\t" + contactDetail[4]);
                  System.out.println("Phone No::\t" + contactDetail[5]);
                  System.out.println("Contact added::\t" + contactDetail[6]);
                }
                break;

                // ORDERING CONTACTS BY CREATED DATE..............
              case 2:
                BufferedReader br = new BufferedReader(new FileReader(contactBookName));
                line4 = "";
                String date = "";
                String[] part;
                TreeMap<Date, String> tm = new TreeMap<Date, String>();
                while ((line4 = br.readLine()) != null) {
                  part = line4.split(":");
                  SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd H:m");
                  String h = (part[6] + ":" + part[7]);
                  Date d = formatter.parse(h);
                  tm.put(d, line4);
                }
                Collection<Date> c1 = tm.keySet();
                // System.out.println(c1);
                Iterator<Date> it1 = c1.iterator();
                if (it1.hasNext()) {
                  System.out.println("\nCONTACTS BELOW ARE LISTED IN ORDER OF CREATED DATES");
                  System.out.println("--------------------------------------------------------");

                } else {
                  System.out.println("NO CONTACTS AVAILABLE");
                }
                while (it1.hasNext()) {
                  String s = it1.next().toString() + "";
                  // System.out.println(s);
                  System.out.println("\n\n" + s);
                  SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd H:m");
                  Date d = formatter.parse(s);
                  System.out.println("---------------------------------");
                  String[] nameContact = tm.get(d).split("=");
                  System.out.println(nameContact[0]);
                  String[] contactDetail = nameContact[1].split(":");
                  for (int i = 0; i < contactDetail.length; i++) {
                    if (contactDetail[i].trim().compareTo("") == 0) {
                      contactDetail[i] = "Unavailable";
                    }
                  }
                  System.out.println("Date of Birth::\t" + contactDetail[0]);
                  System.out.println("Pet name::\t" + contactDetail[1]);
                  System.out.println("Contact Type::\t" + contactDetail[2]);
                  System.out.println("Address::\t" + contactDetail[3]);
                  System.out.println("Email Address::\t" + contactDetail[4]);
                  System.out.println("Phone No::\t" + contactDetail[5]);
                }
                break;

                //// ORDERING CONTACTS BY TAG..............
                // dob:petname:tag:address:email1,email2,email3..:ph1,ph2,...:crtdate
              case 3:
                ArrayList<String> familyList = new ArrayList<String>();
                ArrayList<String> friendsList = new ArrayList<String>();
                ArrayList<String> colleaguesList = new ArrayList<String>();
                ArrayList<String> othersList = new ArrayList<String>();
                line4 = "";
                parts = null;
                br = new BufferedReader(new FileReader(contactBookName));
                while ((line4 = br.readLine()) != null) {
                  parts = line4.split(":");
                  String relation = parts[2];

                  if (relation.equals("Family")) {
                    familyList.add(line4);
                  } else if (relation.equals("Friend")) {
                    friendsList.add(line4);
                  } else if (relation.equals("Associate")) {
                    colleaguesList.add(line4);
                  } else {
                    othersList.add(line4);
                  }
                }
                if (familyList.isEmpty()
                    && friendsList.isEmpty()
                    && colleaguesList.isEmpty()
                    && othersList.isEmpty()) {
                  System.out.println("NO CONTACTS AVAILABLE.");
                } else {
                  System.out.println("\nCONTACTS BELOW ARE LISTED ACCORDING TO TAGS");
                  System.out.println("--------------------------------------------------------");
                }
                if (!familyList.isEmpty()) {
                  System.out.println("\nFAMILY CONTACTS\n----------------------------");
                  for (Object s : familyList) {
                    String[] nameContact = s.toString().split("=");
                    System.out.println(nameContact[0] + "\n");
                    String[] contactDetail = nameContact[1].split(":");
                    for (int i = 0; i < contactDetail.length; i++) {
                      if (contactDetail[i].trim().compareTo("") == 0) {
                        contactDetail[i] = "Unavailable";
                      }
                    }
                    System.out.println("   Date of Birth::\t" + contactDetail[0]);
                    System.out.println("   Pet name::\t\t" + contactDetail[1]);
                    System.out.println("   Address::\t\t" + contactDetail[3]);
                    System.out.println("   Email Address::\t" + contactDetail[4]);
                    System.out.println("   Phone No::\t\t" + contactDetail[5]);
                    System.out.println("   Contact added::\t" + contactDetail[6]);
                  }
                }

                if (!friendsList.isEmpty()) {
                  System.out.println("\nFRIEND CONTACTS\n----------------------------");
                  for (Object s : friendsList) {
                    String[] nameContact = s.toString().split("=");
                    System.out.println(nameContact[0] + "\n");
                    String[] contactDetail = nameContact[1].split(":");
                    for (int i = 0; i < contactDetail.length; i++) {
                      if (contactDetail[i].trim().compareTo("") == 0) {
                        contactDetail[i] = "Unavailable";
                      }
                    }
                    System.out.println("   Date of Birth::\t" + contactDetail[0]);
                    System.out.println("   Pet name::\t\t" + contactDetail[1]);
                    System.out.println("   Address::\t\t" + contactDetail[3]);
                    System.out.println("   Email Address::\t" + contactDetail[4]);
                    System.out.println("   Phone No::\t\t" + contactDetail[5]);
                    System.out.println("   Contact added::\t" + contactDetail[6]);
                  }
                }
                if (!colleaguesList.isEmpty()) {
                  System.out.println("\nASSOCIATE CONTACTS\n----------------------------");
                  for (Object s : colleaguesList) {
                    String[] nameContact = s.toString().split("=");
                    System.out.println(nameContact[0] + "\n");
                    String[] contactDetail = nameContact[1].split(":");
                    for (int i = 0; i < contactDetail.length; i++) {
                      if (contactDetail[i].trim().compareTo("") == 0) {
                        contactDetail[i] = "Unavailable";
                      }
                    }
                    System.out.println("   Date of Birth::\t" + contactDetail[0]);
                    System.out.println("   Pet name::\t\t" + contactDetail[1]);
                    System.out.println("   Address::\t\t" + contactDetail[3]);
                    System.out.println("   Email Address::\t" + contactDetail[4]);
                    System.out.println("   Phone No::\t\t" + contactDetail[5]);
                    System.out.println("   Contact added::\t" + contactDetail[6]);
                  }
                }

                if (!othersList.isEmpty()) {
                  System.out.println("\nOTHER CONTACTS\n----------------------------");
                  for (Object s : othersList) {
                    String[] nameContact = s.toString().split("=");
                    System.out.println(nameContact[0] + "\n");
                    String[] contactDetail = nameContact[1].split(":");
                    for (int i = 0; i < contactDetail.length; i++) {
                      if (contactDetail[i].trim().compareTo("") == 0) {
                        contactDetail[i] = "Unavailable";
                      }
                    }
                    System.out.println("   Date of Birth::\t" + contactDetail[0]);
                    System.out.println("   Pet name::\t\t" + contactDetail[1]);
                    System.out.println("   Address::\t\t" + contactDetail[3]);
                    System.out.println("   Email Address::\t" + contactDetail[4]);
                    System.out.println("   Phone No::\t\t" + contactDetail[5]);
                    System.out.println("   Contact added::\t" + contactDetail[6]);
                  }
                }
                break;

              case 4:
                break;
              default:
                System.out.println("\nENTER ONLY INTEGERS IN THE RANGE 1-5");
                break;
            }
          }
          break;
          // SEARCH CONTACTS BOOK...
        case 5:
          String[] details1 = null;
          line4 = "";
          searchString = "";
          parts = null;
          String addedDetails = "";
          fileReader = new BufferedReader(new FileReader(contactBookName));
          System.out.println("ENTER THE STRING TO BE SERCHED FOR IN THE CONTACTS BOOK");
          searchString = stringReader.nextLine();
          ArrayList<String> nameMatchList = new ArrayList<String>();
          ArrayList<String> emailMatchList = new ArrayList<String>();
          ArrayList<String> tagMatchList = new ArrayList<String>();
          ArrayList<String> addressMatchList = new ArrayList<String>();
          ArrayList<String> dobMatchList = new ArrayList<String>();
          ArrayList<String> phoneMatchList = new ArrayList<String>();
          ArrayList<String> petNameMatchList = new ArrayList<String>();
          String addedString = "";
          while ((line4 = fileReader.readLine()) != null) {
            parts = line4.split("=");
            name = parts[0];
            details1 = parts[1].split(":");
            dateOfBirth = details1[0];
            petName = details1[1];
            contactType = details1[2];
            address = details1[3];
            email = details1[4];
            phoneList = details1[5];
            contactAdded = details1[6];

            addedString = "";
            // System.out.println("name = "+name+" dateOfbirth = "+dateOfBirth+" petName
            // ="+petName+" contactType ="+contactType+"address ="+address+"email
            // ="+email+"phoneList ="+phoneList);

            if (name.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              nameMatchList.add(addedString);
            }

            if (dateOfBirth.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              dobMatchList.add(addedString);
            }

            if (petName.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              petNameMatchList.add(addedString);
            }

            if (contactType.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              tagMatchList.add(addedString);
            }

            if (address.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              addressMatchList.add(addedString);
            }

            if (email.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              emailMatchList.add(addedString);
            }

            if (phoneList.contains(searchString)) {
              addedString =
                  name
                      + "="
                      + dateOfBirth
                      + ":"
                      + petName
                      + ":"
                      + contactType
                      + ":"
                      + address
                      + ":"
                      + email
                      + ":"
                      + phoneList
                      + ":"
                      + contactAdded;
              phoneMatchList.add(addedString);
            }
          }
          System.out.println(
              "\nTOTAL NUMBER OF MATCHES = "
                  + (nameMatchList.size()
                      + dobMatchList.size()
                      + petNameMatchList.size()
                      + tagMatchList.size()
                      + addressMatchList.size()
                      + emailMatchList.size()
                      + phoneMatchList.size()));
          if (!nameMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL " + nameMatchList.size() + " NAMES MATCH WITh " + searchString + "\n");
            System.out.println(
                "\nTHE NAMES OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : nameMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!dobMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL " + dobMatchList.size() + " DOBS' MATCH WITH " + searchString + "\n");
            System.out.println(
                "\nTHE DOB'S OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : dobMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!petNameMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL "
                    + petNameMatchList.size()
                    + " PET NAMES MATCH WITH  "
                    + searchString
                    + "\n");
            System.out.println(
                "\nTHE PET NAMES OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : petNameMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!tagMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL "
                    + tagMatchList.size()
                    + " CONTACT TYPES MATCH WITH THE "
                    + searchString
                    + "\n");
            System.out.println(
                "\nTHE TAGS OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : tagMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!addressMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL "
                    + addressMatchList.size()
                    + " ADDRESSES MATCH WITH "
                    + searchString
                    + "\n");
            System.out.println(
                "\nTHE ADDRESSES OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : addressMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!emailMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL " + emailMatchList.size() + " EMAILS MATCH WITH " + searchString + "\n");
            System.out.println(
                "\nTHE EMAIL'S OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : emailMatchList) {
              showContactByType(s, 2);
            }
          }
          if (!phoneMatchList.isEmpty()) {
            System.out.println(
                "\nTOTAL "
                    + phoneMatchList.size()
                    + " CONTACT NUMBERS MATCH WITH "
                    + searchString
                    + "\n");
            System.out.println(
                "\nTHE PHONE NUMBERS OF THE FOLLOWING CONTACTS MATCHES WITH THE SEARCH STRING ");
            System.out.println(
                "--------------------------------------------------------------------------------\n");

            for (Object s : phoneMatchList) {
              showContactByType(s, 2);
            }
          }

          break;
        case 6:
          break;

        default:
          System.out.println("\nENTER ONLY NUMBERS  RANGED FROM 1-6");
          break;
      }
    }
    // fileReader.close();
    // fileWriter.close();
    // stringReader.close();
    // sc.close();
  }