我的任务是使用选项菜单创建来电呼叫跟踪系统。每个电话都应属于显示用户姓名和电话号码的ArrayList
。我的第一个奋斗是将ArrayList
中的名称(字符串)和数字(双精度)存储在一起。在玩完之后,这就是我想出来的 - 但我的第三课remove
和add
方法不起作用?我做错了什么?我已经在线查看了示例,但我不明白为什么remove
和add
方法不起作用。
我的第三堂课:我的问题在哪里
public class Incoming {
Scanner input = new Scanner(System.in);
ArrayList<Person> arr = new ArrayList<Person>();
Person p1 = new Person("Alex", "01010101");
Person p2 = new Person("Emily", "0123812"); // I will have 10 people
void AddCall() {
System.out.println("Who would you like to add to the call? Enter p+number");
String add = input.nextLine();
Person.add(input);
}
void RemoveCall() {
System.out.println("Which call would you like to answer? Enter p+ caller position"); //NOTE following will be removed from queue
String remove = input.nextLine();
Person.remove(input);
}
void ViewCallerList() {
System.out.println("The queue has the following callers: " + Person);
}
}
答案 0 :(得分:2)
您的Person
课程没有任何名为add
或remove
的方法,因此您无法拨打Person.add
或Person.remove
。相反,您应该在列表中添加和删除项目。
由于您正在从命令提示符处读取调用者数据,因此您必须确定用户键入的文本引用了哪个人。假设他们键入"John,555-5555"
之类的内容,你可以根据它为John构造一个新的Person
对象。使用String#split
,根据逗号的位置拆分文本,然后创建一个新的Person
实例以添加到您的调用者列表中:
public class Incoming {
Scanner input = new Scanner(System.in);
List<Person> callers = new ArrayList<Person>();
Person p1 = new Person("Alex", "01010101");
Person p2 = new Person("Emily", "0123812"); // I will have 10 people
private static Person readPerson(Scanner sc) {
String callerText = sc.nextLine();
String[] callerData = callerText.split(",");
return new Person(callerData[0], callerData[1]);
}
void addCall() {
System.out.println("Who would you like to add to the call? Enter p+number");
callers.add(readPerson(input));
}
void removeCall() {
// Complete this based on the add method above
}
// This should output the list (callers), not a single person
void viewCallerList() {
System.out.println("The queue has the following callers: " + callers);
}
}