java个人电话簿代码 个人通讯录java代码( 二 )


String cellphone = scn.nextLine();
int index = -1;
for (int i = 0; isizeicontacts.length;
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该记录不存在!");
} else {
System.out.print("请输入姓名:");
String name = scn.nextLine();
contacts[index].name = name;
}
}
} while (!cmd.equals("0"));
System.out.println("退出成功!");
scn.close();
System.exit(0);
}
}
java的电话簿程序==================================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class PhoneBook {
// 代表有多少条记录
private intsize= 0;
// 用来记录信息的数组
private Phone[] phones= new Phone[100];
private String filename = "phonebook.txt";
public PhoneBook() {
try {
read();
} catch (IOException e) {
}
}
private void read() throws IOException {
File f = new File(filename);
if (!f.exists()) {
return;
}
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = br.readLine()) != null) {
if (line.trim().length()0) {
Phone phone = new Phone(line);
addPhone(phone);
}
}
br.close();
}
public void store() throws IOException {
File f = new File(filename);
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
for (Phone phone : phones) {
if (phone == null) {
continue;
}
String str = phone.name + "::" + phone.number + "::" + phone.notes;
bw.write(str + "\r\n");
}
bw.close();
}
public void addPhone(Phone phone) {
phones[size++] = phone;
}
public Phone getPhone(String name) {
for (Phone phone : phones) {
if (phone == null) {
continue;
}
if (phone.name.equalsIgnoreCase(name)) {
return phone;
}
}
return null;
}
public Phone[] getPhones() {
return phones;
}
}
class Phone {
String name, number, notes;
public Phone() {
}
public Phone(String line) {
String[] strs = line.split("::");
name = strs[0];
number = strs[1];
notes = strs[2];
}
public String toString() {
return String.format("-- %s\r\n-- %s\r\n-- %s", name, number, notes);
}
}
=================================================
import java.io.IOException;
import java.util.Scanner;
public class MainClass {
private static PhoneBook book = new PhoneBook();
public static void main(String[] args) {
getCommand();
}
public static void getCommand() {
String cmd = getString("Command: ");
if (cmd.startsWith("e ")) {
add(cmd.substring(cmd.indexOf(' ') + 1));
try {
book.store();// 添加一个就记录一次文件
} catch (IOException e) {
e.printStackTrace();
}
} else if (cmd.startsWith("f ")) {
find(cmd.substring(cmd.indexOf(' ') + 1));
} else if (cmd.equals("l")) {
list();
} else if (cmd.startsWith("q")) {
quit();
} else {
System.out.println("unknown command!");
}
getCommand();
}
public static void add(String name) {
Phone phone = new Phone();
phone.name = convert(name);// 名字转换
phone.number = getString("Enter number: ");
phone.notes = getString("Enter notes: ");
book.addPhone(phone);
}
public static void find(String name) {
Phone phone = book.getPhone(name);

推荐阅读