import java.io.*;
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;
public class ContactListv3 {
private static String DATA_FILE_NAME = ".contactlist";
public static void main(String[] args) {
String firstname,lastname ,number, age , gender, height ;
TreeMap<String,String > contactList;
contactList = new TreeMap<String,String>();
File userHomeDirectory = new File( System.getProperty("user.home") );
File dataFile = new File( userHomeDirectory, DATA_FILE_NAME );
if ( ! dataFile.exists() ) {
System.out.println("No contact data file found.");
System.out.println("A new one will be created.");
System.out.println("File name: " + dataFile.getAbsolutePath());
}
else {
System.out.println("Reading contact data...");
try {
Scanner scanner = new Scanner( dataFile );
while (scanner.hasNextLine()) {
String phoneEntry = scanner.nextLine();
int separatorPosition = phoneEntry.indexOf('%');
if (separatorPosition == -1)
throw new IOException("File is not a contact data file.");
firstname = phoneEntry.substring(0, separatorPosition);
number = phoneEntry.substring(separatorPosition+1);
contactList.put(firstname,number);
lastname =phoneEntry.substring(0, separatorPosition);
age = phoneEntry.substring(separatorPosition+1);
contactList.put(lastname,age);
height = phoneEntry.substring(0, separatorPosition);
gender = phoneEntry.substring(separatorPosition+1);
contactList.put(height,gender);
}
}
catch (IOException e) {
System.out.println("Error in contact list data file.");
System.out.println("File name: " + dataFile.getAbsolutePath());
System.out.println("This program cannot continue.");
System.exit(1);
}
}
Scanner in = new Scanner( System.in );
boolean changed = false;
mainLoop: while (true) {
System.out.println("####################################");
System.out.println("Year/Section: <2018/1A>");
System.out.println("Date Submitted: <09/16/2018>");
System.out.println("######### My Contact List ##########");
System.out.println(" 1. Look up a Contact.");
System.out.println(" 2. Add or change a Contact.");
System.out.println(" 3. Remove an entry Contact.");
System.out.println(" 4. List the entire Contact directory.");
System.out.println(" 5. Exit from the program.");
System.out.println("Enter action number (1-5): ");
int command;
if ( in.hasNextInt() ) {
command = in.nextInt();
in.nextLine();
}
else {
System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.");
in.nextLine();
continue;
}
switch(command) {
case 1:
System.out.print("\nEnter the First name whose details you want to look up: ");
number = in.nextLine().trim().toLowerCase();
firstname = in.nextLine().trim().toLowerCase();
lastname = in.nextLine().trim().toLowerCase();
age = in.nextLine().trim().toLowerCase();
height = in.nextLine().trim().toLowerCase();
gender = in.nextLine().trim().toLowerCase();
firstname = contactList.get(number+firstname+lastname+gender+age+height);
if (firstname == null)
System.out.println("\nSORRY, NO RECORD FOUND FOR ");
else
System.out.println("\n DETAILS FOR "+ firstname + " "+ lastname + gender + age + height + number );
break;
case 2:
System.out.print("\nEnter the First name: ");
firstname = in.nextLine().trim().toLowerCase();
if (firstname.length() == 0)
System.out.println("\nNAME CANNOT BE BLANK.");
else if (firstname.indexOf('%') >= 0)
System.out.println("\nNAME CANNOT CONTAIN THE CHARACTER \"%\".");
System.out.print("\nEnter the Last name: ");
lastname = in.nextLine().trim().toLowerCase();
if (lastname.length() == 0)
System.out.println("\nNAME CANNOT BE BLANK.");
else if (lastname.indexOf('%') >= 0)
System.out.println("\nNAME CANNOT CONTAIN THE CHARACTER \"%\".");
System.out.print("\nEnter Age: ");
age = in.nextLine().trim().toLowerCase();
if (age.length() == 0)
System.out.println("\nAGE CANNOT BE BLANK.");
else if (age.indexOf('%') >= 0)
System.out.println("\nNAME CANNOT CONTAIN THE CHARACTER \"%\".");
System.out.print("\nEnter height: ");
height = in.nextLine().trim().toLowerCase();
if (height.length() == 0)
System.out.println("\nHEIGHT CANNOT BE BLANK.");
else if (height.indexOf('%') >= 0)
System.out.println("\nNAME CANNOT CONTAIN THE CHARACTER \"%\".");
System.out.print("\nEnter the Gender: ");
gender = in.nextLine().trim().toLowerCase();
if (gender.length() == 0)
System.out.println("\nNAME CANNOT BE BLANK.");
else if (gender.indexOf('%') >= 0)
System.out.println("\nNAME CANNOT CONTAIN THE CHARACTER \"%\".");
else {
System.out.println(" ");
System.out.print("Enter phone number: ");
number = in.nextLine().trim();
if (number.length() == 0)
System.out.println("\nPHONE NUMBER CANNOT BE BLANK.");
else {
contactList.put(firstname,lastname);
contactList.put(number,gender);
contactList.put(height,age);
changed = true;
}
}
break;
case 3:
System.out.print("\nEnter the name whose entry you want to remove: ");
firstname = in.nextLine().trim().toLowerCase();
number = contactList.get(firstname);
if (number == null)
System.out.println("\nSORRY, THERE IS NO ENTRY FOR " + firstname);
else {
contactList.remove(firstname);
changed = true;
System.out.println("\nDIRECTORY ENTRY REMOVED FOR " + firstname );
}
break;
case 4:
System.out.println("\nLIST OF ENTRIES IN YOUR Contact List:\n");
number = in.nextLine().trim().toLowerCase();
firstname = in.nextLine().trim().toLowerCase();
lastname = in.nextLine().trim().toLowerCase();
age = in.nextLine().trim().toLowerCase();
height = in.nextLine().trim().toLowerCase();
gender = in.nextLine().trim().toLowerCase();
firstname = contactList.get(firstname);
lastname = contactList.get(lastname);
number= contactList.get(number);
age = contactList.get(age);
height= contactList.get(height);
gender = contactList.get(gender);
System.out.println(firstname);
System.out.println(lastname);
System.out.println(number);
System.out.println(age);
System.out.println(gender);
System.out.println(height);
break;
case 5:
System.out.println("\nExiting program.");
break mainLoop;
default:
System.out.println("\nILLEGAL ACTION NUMBER.");
}
}
if (changed) {
System.out.println("Saving contact list changes to file " +
dataFile.getAbsolutePath() + " ...");
PrintWriter out;
try {
out = new PrintWriter( new FileWriter(dataFile) );
}
catch (IOException e) {
System.out.println("ERROR: Can't open data file for output.");
return;
}
for ( Map.Entry<String,String> entry : contactList.entrySet() )
out.println(entry.getKey() + "%" + entry.getValue() );
out.close();
if (out.checkError())
System.out.println("ERROR: Some error occurred while writing data file.");
else
System.out.println("Done.");
}
}
}
Я немного новичок в программировании примерно через месяц, сейчас, когда я запускаю свой код и пытаюсь найти контакт, он показывает только данные по имени и фамилии или иногда он становится нулевым. И когда я пытаюсь просмотреть весь список моих контактов, он просто идет в этом формате.
год: возраст
высота: пол
имя: фамилия
1008 * Firstname *
фамилия
номер
возраст
высота
пол
Любая помощь будет признательна, особенно если eli5.