简体   繁体   English

读取文本文件并将其添加到Java中的对象的ArrayList中

[英]Read and Add Text File to an ArrayList of Object in Java

I have a text file "addresses.txt" that holds information about a person. 我有一个文本文件“ addresses.txt”,其中包含有关一个人的信息。 I have created a Person class and I need to read and store the information from this text file into an ArrayList. 我已经创建了一个Person类,并且需要读取该文本文件中的信息并将其存储到ArrayList中。 My error is when trying to read the text file I can not add it to my ArrayList because of the String arguement. 我的错误是尝试读取文本文件时,由于字符串争论,我无法将其添加到ArrayList中。 Really lost at this point I know it may be a simple solution but I just cant figure it out. 在这一点上我真的迷失了,我知道这可能是一个简单的解决方案,但我只是想不通。

Here is some of my Person class if needed: 如果需要,这是一些我的Person类:

public class Person {
private String firstName;
private String lastName;
private String phoneNumber;
private String address;
private String city;
private String zipCode;

private static int contactCounter = 0;

public Person(String firstName, String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
    contactCounter++;
}

public Person(String firstName, String lastName, String phoneNumber){
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    contactCounter++;
}

public Person(String firstName, String lastName, String address, String city, String zipCode){
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.city = city;
    this.zipCode = zipCode;
    contactCounter++;
}

Here is my main class: 这是我的主要课程:

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Rolodex {

public static void main(String[] args) {
    ArrayList <Person> contactList = new ArrayList <Person> ();
    readFile(contactList);
}

public static void readFile(ArrayList <Person> contactList){
    try{
        Scanner read = new Scanner(new File("addresses.txt"));
        do{
            String line = read.nextLine();
            contactList.add(line); //MY ERROR IS HERE. I know why its happening just not how to fix.
        }while(read.hasNext());
        read.close();
    }catch(FileNotFoundException fnf){
        System.out.println("File was not found.");
    }
}

You try to add String line into Person array. 您尝试将字符串行添加到Person数组。 You can't cast String to Person. 您不能将String强制转换为Person。 Fix:Try to implement some kind of line parser eg. 修复:尝试实现某种行解析器,例如。 Your line looks like this "adam;bra;555888666;" 您的行看起来像这样的"adam;bra;555888666;" you have to parse this string using line.split(";") it creates you array of Strings (String[]) now just use your constructors to create Person and add him into contactList eg. 您必须使用line.split(";")解析此字符串,它会创建您的字符串数组(String []),现在只需使用构造函数创建Person并将其添加到contactList中即可。

contactList.add(New Person(parsedString[0], parsedString[1], parsedString[2]));

Instead of using a text file, you should use a JSON file. 您应该使用JSON文件,而不是使用文本文件。 And with the GSON library, it's more easy to get and write data in files... 使用GSON库,可以更轻松地在文件中获取和写入数据...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM