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);
推荐阅读
- 苹果手机安卓手机直播声卡,苹果手机直播声卡哪个牌子好
- 荣耀v30升级鸿蒙跑分的简单介绍
- jqueryonbulr的简单介绍
- 微信群直播小助手ios,微信群直播小程序
- c语言中怎么计算三角函数 c语言求三角函数的程序
- python循环函数的结束,python 循环结束
- iphone上的飞行游戏,苹果游戏飞行游戏叫什么
- c语言如何写子函数调用 c语言子函数怎么调用子函数
- 纯黑直播什么,纯黑直播什么好处