public static void main(String[] args) { int i = 4; double d = 4.0; String s = "HackerRank "; Scanner scan = new Scanner(System.in); /* Declare second integer, double, and String variables. */ int j; double e; String t; /* Read and save an integer, double, and String to your variables.*/ j = scan.nextInt(); e = scan.nextDouble(); scan.nextLine(); t = scan.nextLine(); /* Print the sum of both integer variables on a new line. */ System.out.println(i + j); /* Print the sum of the double variables on a new line. */ System.out.println(d + e); /* Concatenate and print the String variables on a new line; the 's' variable above should be printed first. */ System.out.println(s + t); scan.close(); }
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 mapSize = Integer.parseInt(sc.nextLine()); char[][] map = new char[mapSize][]; for (int i = 0; i < mapSize; i++) { String numbersString = sc.nextLine(); map[i] = numbersString.toCharArray(); } printMapWithCavities(map, mapSize); }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = Integer.parseInt(in.nextLine()); for (int a = 0; a < T; a++) { String PAN = in.nextLine(); System.out.println(isValidPAN(PAN)); } in.close(); }
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[][] numArray = new String[6][]; int largestSum = 0; for (int i = 0; i < 6; i++) { numArray[i] = sc.nextLine().split(" "); } for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { int sum = Integer.parseInt(numArray[i][j]) + Integer.parseInt(numArray[i][j + 1]) + Integer.parseInt(numArray[i][j + 2]) + Integer.parseInt(numArray[i + 1][j + 1]) + Integer.parseInt(numArray[i + 2][j]) + Integer.parseInt(numArray[i + 2][j + 1]) + Integer.parseInt(numArray[i + 2][j + 2]); // System.out.println(sum); if (i == 0 && j == 0) { largestSum = sum; } else { if (sum > largestSum) { largestSum = sum; } } } } System.out.println(largestSum); }
private static void displayPhoneBooks(ArrayList<String> phoneBooks) throws IOException, ParseException { if (phoneBooks.size() == 0) { System.out.println("\nNO PHONEBOOK AVAILABLE.CREATE A NEW PHONEBOOK.\n"); return; } else { System.out.println("FOLLOWING ARE THE AVAILABLE PHONEBOOKS."); System.out.println("----------------------------------------\n"); Scanner sc = new Scanner(System.in); for (int i = 0; i < phoneBooks.size(); i++) { String s = phoneBooks.get(i); System.out.println(s + "\t->PRESS " + (i + 1) + " LOAD THIS."); } System.out.println("TO GO BACK TO THE PREVIOUS MENU->PRESS " + (phoneBooks.size() + 1)); int ch = 0; while (ch < 1 || ch > (phoneBooks.size() + 1)) { while (!sc.hasNextInt()) { System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY.."); sc.nextLine(); } ch = sc.nextInt(); if (ch < 1 || ch > phoneBooks.size()) System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY.."); } if (ch == (phoneBooks.size() + 1)) return; contactBookOperations(phoneBooks.get(ch - 1) + ".txt"); } }
/** @param args the command line arguments */ public static void main(String[] args) { // Takes user input and converts into a character array Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); String input = sc.nextLine(); String[] a = input.split(" "); BigInteger[] storage = new BigInteger[n]; BigInteger sum = new BigInteger("0"); // Converts and Stores Big Integers for (int i = 0; i < n; i++) storage[i] = new BigInteger(a[i]); // Sums the BigIntegers in storage for (int j = 0; j < n; j++) sum = sum.add(storage[j]); // Outputs the sum of values in storage as a string System.out.println(sum); }
public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); // final String fileName = System.getenv("OUTPUT_PATH"); // BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); int res; int _arr_size = Integer.parseInt(in.nextLine()); int[] _arr = new int[_arr_size]; int _arr_item; for (int _arr_i = 0; _arr_i < _arr_size; _arr_i++) { _arr_item = Integer.parseInt(in.nextLine()); _arr[_arr_i] = _arr_item; } res = lonelyInteger(_arr); System.out.println(res); // bw.write(String.valueOf(res)); // bw.newLine(); // bw.close(); }
public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); while (scanner.hasNextLine()) { String str = scanner.nextLine(); if (str.equals("0")) break; String s[] = str.split(" "); int sum = 1; for (int i = 0; i < s.length; i += 2) sum = sum * (int) Math.pow(Integer.parseInt(s[i]), Integer.parseInt(s[i + 1])); Factorization f = new Factorization(); System.out.println(f.answer(sum - 1)); } }
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 in = new Scanner(System.in); String input = ""; int n = 1; String output = ""; while (in.hasNextLine()) { if (in.hasNext()) { output += n + " " + in.nextLine() + "\n"; n++; } } System.out.println(output); }
public static void main(String[] args) throws FileNotFoundException, IOException { JOptionPane myWindow; myWindow = new JOptionPane(); String fileName; fileName = myWindow.showInputDialog("Enter External Text File to Open;"); Scanner scanner = new Scanner(new File(fileName)); JTextArea outputOriginalFile = new JTextArea(); outputOriginalFile.setText("The Original File was:\n"); JTextArea outputTextArea = new JTextArea(); while (scanner.hasNext()) { String fullName = scanner.nextLine(); outputOriginalFile.append(fullName + "\n"); } myWindow.showMessageDialog( null, outputOriginalFile, "Opened file....", myWindow.INFORMATION_MESSAGE); PrintWriter pw = new PrintWriter(new File("outputOfFile.txt")); String firstName, middleName, lastName; JTextArea outputTextArea1 = new JTextArea(); outputTextArea1.setText("The Modified Text File is:\n"); Scanner scanner2 = new Scanner(new File(fileName)); while (scanner2.hasNext()) { String fullName = scanner2.nextLine(); StringTokenizer st = new StringTokenizer(fullName, " "); firstName = st.nextToken(); middleName = st.nextToken(); lastName = st.nextToken(); pw.println(lastName + ", " + firstName + " " + middleName.substring(0, 1) + "."); outputTextArea1.append( lastName + ", " + firstName + " " + middleName.substring(0, 1) + "." + "\n"); } myWindow.showMessageDialog(null, outputTextArea1, "Results....", myWindow.INFORMATION_MESSAGE); scanner.close(); scanner2.close(); pw.close(); }
private String getFileWords(String s) { String output = ""; try { // THIS CODE SEGMENT WILL NOT RUN IN AN APPLET THAT IS NOT "SIGNED" // By default applets cannot alter files on the hard drive of the user. // If the panel is opened in a regular application the save button should work. // This can be tested in HomeworkApplication.java through HomeworkRun.java only File inputFile = new File(s); Scanner inputScanner = new Scanner(inputFile); while (inputScanner.hasNext()) { output = output + inputScanner.nextLine() + "\n"; } inputScanner.close(); String numWords = getWords(output); return numWords; } catch (IOException e) { outputLabel.append(e + ""); } return output; }
public static void main(String[] args) { Scanner sc = null; Scanner stringReader = null; try { int ch = 0; sc = new Scanner(System.in); boolean validFile = false; stringReader = new Scanner(System.in); System.out.println("\n\t\t\tCONTACTS APP"); System.out.println( "--------------------------------------------------------------------------------"); while (ch != 5) { System.out.println("\nTO CREATE PHONEBOOK->PRESS 1"); System.out.println("TO LOAD A CREATED PHONEBOOK->PRESS 2"); System.out.println("TO DELETE CREATED PHONEBOOK->PRESS 3"); System.out.println("TO LIST ALL PHONEBOOKS->PRESS 4"); System.out.println("TO EXIT->PRESS 5"); while (!sc.hasNextInt()) { System.out.println("\nONLY NUMBERS RANGED FROM 1 - 5 SHOULD BE ENTERED..RETRY"); sc.nextLine(); } ch = sc.nextInt(); switch (ch) { case 1: System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook).DONT GIVE ANY EXTENSION."); validFile = false; String contactBookName = ""; while (!validFile) { contactBookName = stringReader.nextLine(); if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME MUST NOT BE EMPTY"); System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook).DONT GIVE ANY EXTENSION."); continue; } if (!(contactBookName.contains("/") || contactBookName.contains("\\") || contactBookName.contains(":") || contactBookName.contains("*") || contactBookName.contains("?") || contactBookName.contains("/") || contactBookName.contains("\"") || contactBookName.contains("<") || contactBookName.contains(">") || contactBookName.contains("|"))) { String extension[] = contactBookName.split("."); contactBookName = contactBookName + ".txt"; File contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nNEW PHONEBOOK CREATED::" + contactBookName); validFile = true; } else { System.out.println( "\nTHIS PHONEBOOK NAMED " + contactBookName + " ALREADY EXISTS\nPLEASE ENTER A DIFFERENT NAME"); } } else { System.out.println("\nFILE NAME CANNOT CONTAIN : /,\\,\",*,?,<,>,|"); System.out.println( "\nENTER NAME OF CONTACTS BOOK TO CREATE WITHOUT FILE EXTENSION "); } } contactBookOperations(contactBookName); break; case 2: contactBookName = ""; System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook) TO LOAD.DONT GIVE ANY EXTENSION."); contactBookName = stringReader.nextLine(); while (true) { if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME MUST NOT BE EMPTY "); } else { break; } } contactBookName += ".txt"; File contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nA PHONEBOOK WITH NAME " + contactBookName + " DOES NOT EXIST"); } else { contactBookOperations(contactBookName); } break; case 3: contactBookName = ""; System.out.println("\nENTER NAME OF PHONEBOOK WITHOUT EXTENSION TO DELETE"); contactBookName = stringReader.nextLine(); if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME CANNOT BE EMPTY "); } contactBookName += ".txt"; contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nA PHONEBOOK WITH NAME " + contactBookName + " DOES NOT EXIST"); } else { contactBook.delete(); System.out.println("\nA PHONEBOOK " + contactBookName + " DELETED"); } break; case 4: String curDirectory = System.getProperty("user.dir"); listPhoneBooks(curDirectory); break; case 5: System.out.println("\nSUCCESSFULLY EXITED"); break; default: System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1 - 5"); break; } } } catch (Exception e) { System.out.println("AN ERROR OCCURED!!"); e.printStackTrace(); } finally { if (sc != null) sc.close(); if (stringReader != null) stringReader.close(); } }
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(); }
public static void main(String[] args) { int months[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; System.out.println("What is your birthday? (MM/dd/yyyy)"); Scanner scanner = new Scanner(System.in); String birthDay = scanner.nextLine(); System.out.println("What is your name?"); String name = scanner.nextLine(); String[] parts = birthDay.split("/"); Calendar time = Calendar.getInstance(); int ageMillenniums = 0; int ageCenturies = 0; int ageDecades = 0; String stringMillenniums = ""; String stringCenturies = ""; String stringDecades = ""; String stringYears = ""; String stringMonths = ""; String stringDays = ""; String stringHours = ""; String stringMinutes = ""; String stringSeconds = ""; int day = (time.get(Calendar.DATE)); int month = (months[time.get(Calendar.MONTH)]); int year = (time.get(Calendar.YEAR)); int hour = (time.get(Calendar.HOUR)); int minute = (time.get(Calendar.MINUTE)); int second = (time.get(Calendar.SECOND)); int ageDays = day - Integer.parseInt(parts[1]); int ageMonths = month - Integer.parseInt(parts[0]); int ageYears = year - Integer.parseInt(parts[2]); if (ageDays < 0) { if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1) { ageMonths--; ageDays += 31; } else if (month == 5 || month == 7 || month == 10 || month == 12) { ageMonths--; ageDays += 30; } else { ageMonths--; ageDays += 28; } } if (ageMonths < 0) { ageYears--; ageMonths += 12; } while (ageYears > 1000) { ageYears -= 1000; ageMillenniums++; } while (ageYears > 100) { ageYears -= 100; ageCenturies++; } while (ageYears > 10) { ageYears -= 10; ageDecades++; } if (ageMillenniums == 0) { stringMillenniums = ""; } else if (ageMillenniums == 1) { stringMillenniums = ageMillenniums + " millennium, "; } else { stringMillenniums = ageMillenniums + " millenniums, "; } if (ageCenturies == 0) { stringCenturies = ""; } else if (ageCenturies == 1) { stringCenturies = ageCenturies + " century, "; } else { stringCenturies = ageCenturies + " centuries, "; } if (ageDecades == 0) { stringDecades = ""; } else if (ageDecades == 1) { stringDecades = ageDecades + " decade, "; } else { stringDecades = ageDecades + " decades, "; } if (ageYears == 0) { stringYears = ""; } else if (ageYears == 1) { stringYears = ageYears + " year, "; } else { stringYears = ageYears + " years, "; } if (ageMonths == 0) { stringMonths = ""; } else if (ageMonths == 1) { stringMonths = ageMonths + " month, "; } else { stringMonths = ageMonths + " months, "; } if (ageDays == 0) { stringDays = ""; } else if (ageDays == 1) { stringDays = ageDays + " day, "; } else { stringDays = ageDays + " days, "; } if (hour == 0) { stringHours = ""; } else if (hour == 1) { stringHours = hour + " hour, "; } else { stringHours = hour + " hours, "; } if (minute == 0) { stringMinutes = ""; } else if (minute == 1) { stringMinutes = minute + " minute, "; } else { stringMinutes = minute + " minutes, "; } if (second == 0) { stringSeconds = ""; } else if (second == 1) { stringSeconds = second + " second "; } else { stringSeconds = second + " seconds "; } System.out.println( name + " is " + stringMillenniums + stringCenturies + stringDecades + stringYears + stringMonths + stringDays + stringHours + stringMinutes + stringSeconds + "old"); }