【问题标题】:Reading Text file and adding the objects into an Array读取文本文件并将对象添加到数组中
【发布时间】:2017-07-14 13:56:03
【问题描述】:

我有一个 Contact 类,它基本上只保存联系人的信息。我们将联系人对象存储在一个数组中。

我必须将数组写入一个文本文件,而且我知道该怎么做,但现在我必须读取该文件并将对象存储回一个数组中,但我被卡住了!

注意,下面的ContactList 也使用了Contact 类,它基本上只有get/set 方法。

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;

public class ContactList{
    int ptr = -1;   
    Contact[] list;
    int contactLength;
    public ContactList(){//second constructor needed
        list=new Contact[20];
        contactLength=20;
        for(int i =0;i<20;i++){
            list[i]=null;
        }
    }
    public ContactList(int length){//second constructor needed
        list=new Contact[length];
        contactLength=length;
        for(int i =0;i<length;i++){
            list[i]=null;
        }
    }
    public boolean add(Contact c){
        boolean found = false;
        int i = 0;
        while(!found&&i<20){
            if (list[i]==null){
                list[i]=c;
                found=true;
                ptr=i;
            }
            i++;
        }
        return found;
    }
    public Contact find(String name){
        boolean found=false;
        int i =0;
        while(i<contactLength&&!found){
            ptr++;
            if(ptr==contactLength){
                ptr=0;
            }
            if(list[ptr]!=null){
                if (list[ptr].getName().contains(name)){
                    found=true;
                    return list[ptr];
                }
            }   
            i++;
        }
        return null;
    }
    public Contact remove(){
        Contact current= list[ptr];
        list[ptr]=null;
        return current;
    }
    public void displayContacts(){
        for(int i =0;i<contactLength;i++){
            if(list[i]!=null){
                System.out.println(list[i].toString());
            }
            else {
                System.out.println("Empty:");//"Name:\nAddress:\nPhone\nComments:"
            }
        }
    }



public boolean write (String fileName){
    PrintWriter p = null; 
    try {
p = new PrintWriter(new File(fileName));
} catch (Exception e) {
return false;
}
for(int i =0;i<contactLength;i++){
    if(list[i]!=null){
        p.println(list[i].toString());
    }}
    p.close();

return true; 
}
public class Contact {
  private String name;
  private long phone;
  private String address;
  private String comments;

  public void setName( String name){
    this.name =name;
  }

  public String getName(){
    return name;
  }

  public void setPhone(long phone){
    this.phone=phone;
  }

  public long getPhone(){
    return phone;
  }

  public void setAddress(String address){
    this.address= address;
  }

  public String getAddress(){
    return address;
  }

  public void setComments( String comments){
    this.comments= comments;
  }

  public String getComments(){
    return comments;
  }


  public String toString(){
    return ("Name:\t\t"+name+"\nAddress:\t"+address+"\nPhone Number:\t"+phone+"\nComments:\t"+comments +"\n");
  }

  public Contact(String name, long phone, String address, String comments){
    this.name=name;
    this.phone=phone;
    this.address=address;
    this.comments=comments;
  }
  public boolean equals(Contact other){
    if (this.name!=other.name){
      return false;
    }
    if (this.phone!=other.phone){
      return false;
    }
    if (this.address!=other.address){
      return false;
    }
    if (this.comments!=other.comments){
      return false;
    }
    return true;
  }

这是我目前所拥有的......

public boolean read(String fileName){


Scanner s = null; 
try {
s = new Scanner(new File(fileName));
} catch (Exception e) { // returns false if fails to find fileName
return false;
}

for(int i=0; i)

}

是的,我必须使用数组!没有清单!没有什么花哨的,这是一个介绍类,我不会明白的。只是扫描仪。

【问题讨论】:

  • 你基本上已经发布了,“这里有一些一般要求,这里有一些无法解释的代码。哦,请给我没有列表的代码......”。请。在提问中付出一些努力,包括 - 显示您尝试解决问题的地方,只是不符合您的尝试,只是哪些概念让您感到困惑。
  • @HovercraftFullOfEels Smh,没有什么能让你们高兴。如果我不提供代码,他们会说,“我们没有看到你的代码,或者你什么也没写。”我提供了所有代码,因此很容易理解,并在我卡住的地方崩溃了......让我休息一下
  • 您选择了一种无法可靠解析的格式(除非对联系人的元素可以包含的内容有严格的限制)。您最好使用 JSON 或 XML 来保存联系人。解析回联系人列表很简单。例如,假设一个联系人的 cmets 包含另一个联系人的 String 表示。你怎么能解析它?
  • @MSK:与其摇头,不如考虑更详细地解释您的问题。当然,你不能因为有人要求这个而责备任何人,现在可以吗?
  • 您可以简单地提供您尝试阅读的文件的简短示例,以及到目前为止您为阅读它而编写的代码。我们不需要查看您的 Contact 类或最初编写该文件的代码。只是,“我正在尝试用姓名、地址、电话号码和 cmets 解析这个文本文件。这是我为解析它们而编写的代码,但我不能......”和你的具体部分遇到问题。

标签: java arrays object


【解决方案1】:

我在评论部分看到了利弊,结束辩论怎么样,考虑到你的写作方式,我猜你需要这样的东西:

   public static void read(String fileName){
     try(BufferedReader in = new BufferedReader(new FileReader(fileName))){
         String line = null;
         String[] contact = new String[4];
         int contactCounter = 0;
         int buffer = 0;
         while ((line = in.readLine()) != null){
             buffer = line.split("\t").length - 1;
             contact[contactCounter] = line.split("\t")[buffer];
             contactCounter++;
             if (contactCounter == 3){
                 new Contact(contact[0], contact[1], contact[2], contact[3]);
                 contactCounter == 0;
             }

         }

     } catch (IOException e){
         e.printStackTrace();
     }
}

我强烈建议您改进您的联系人序列化方式,因为这种格式一团糟,尤其是没有明确的界限,我能想到的最好的办法就是数 4 EOL 来创建一个新的联系人。 p>

也许看看 csv 格式:https://en.wikipedia.org/wiki/Comma-separated_values

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多