首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhoneBook类未正确保存Entry对象

PhoneBook类未正确保存Entry对象
EN

Stack Overflow用户
提问于 2014-09-20 09:48:05
回答 1查看 349关注 0票数 1

我有一个程序,可以读取包含名字、姓氏和电话号码的文件,格式如下: John Doe 123-456-7890

程序将这些字符串作为字符串读取,并将它们输入到Entry类中,然后将Entry对象添加到PhoneBook中。现在,当I打印时,它只是打印最后一个条目56次(在txt文件中有56个“条目”)。不管出于什么原因,条目都被正确地传递给了entry类,因为我可以正确地打印这些条目,但是当我将它们添加到PhoneBook中并尝试打印它时,只打印最后一个条目。如果我把这件事搞砸了,我将不胜感激。谢谢!

代码语言:javascript
复制
import java.util.Scanner;
import java.io.*;

public class main{

public static void main(String[] args) throws FileNotFoundException{
    String firstName, lastName, fullName, phoneNumber;
    Entry entry = new Entry();
    PhoneBook phonebook = new PhoneBook();

    File myFile = new File("C:\\Users\\Gregory\\Desktop\\Names_Numbers.txt");
    Scanner infile = new Scanner(myFile);

    //check if file exists
    if (!myFile.exists()){
        System.out.println("File not able to be found");
        System.exit(0);
    }

    while (infile.hasNext()){
        firstName=infile.next();
        lastName=infile.next();
        fullName=firstName+ " " + lastName;
        phoneNumber=infile.next();
        entry.setName(fullName);
        entry.setPhoneNumber(phoneNumber);
        phonebook.add(entry); 
    }

    phonebook.print();
    infile.close();


}
}`

入口类`

代码语言:javascript
复制
public class Entry {
private String name;
private String phoneNumber;

public Entry() {
    name = " ";
    phoneNumber = " ";
}
public Entry(String n, String pN) {
    name=n;
    phoneNumber=pN;
}
public void setName(String fullName) {
    name=fullName;
}
public void setPhoneNumber(String pN){
    phoneNumber=pN;
}
public String getName() {
    return name;
}
public String getPhoneNumber(){
    return phoneNumber;
}
public void print(){
    System.out.println(this.name + " " + this.phoneNumber);
}
}

`

代码语言:javascript
复制
public class PhoneBook {
private static final int CAPACITY = 100;
private Entry entry[]=new Entry[CAPACITY];
private int count; //holds current amount of entries in book

public PhoneBook(){
    count=0; //set initial count value
}
public void add(Entry newEntry){
    if (count > CAPACITY)
        System.out.println("Maximum capacity reached, no more adds allowed");
    entry[count]=newEntry;
    count++;
}
public String find(String pN){
     for (int i=0;i<count;i++) {
            if (entry[i].getPhoneNumber()==pN)
                return entry[i].getName();
        }
        String error="Name not found in phone book.";
        return error;
}
public void print(){
    for (int i=0; i<count;i++)
        entry[i].print();
}
}`
EN

回答 1

Stack Overflow用户

发布于 2014-09-20 10:25:07

在主类的While循环中:

代码语言:javascript
复制
     while (infile.hasNext()){
            firstName=infile.next();
            lastName=infile.next();
            fullName=firstName+ " " + lastName;
            phoneNumber=infile.next();
            entry = new Entry(fullName, phoneNumber);
            phonebook.add(entry); 
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25944420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档