如何通过数组传递类,然后使用txt文档填充数组

时间:2016-04-14 14:42:07

标签: java

所以...我的目标是通过一个数组运行这个类然后用一个包含43个实例的.txt文档填充数组,然后我将获取用户数据并比较两者以找到理想的匹配。我应该注意这是一个座位计划 - 文本文件看起来像这样 -

01 STANDARD True False True F False

public class Seat {

    private  String eMail = "";
    private int number;
    private String type;
    private boolean window;
    private boolean aisle;
    private boolean table;
    private String f;
    private String b;
    private boolean ease;

    public Seat(int number, String type, boolean window, boolean aisle, boolean table, String f, String b, boolean ease) {
        this.number = number;
        this.type = type;
        this.window = window;
        this.aisle = aisle;
        this.table = table;
        this.f = f;
        this.b = b;
        this.ease = ease;
    }
    public String geteMail() {
        return eMail;
    }
    public void seteMail(String eMail) {
        this.eMail = eMail;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public boolean isWindow() {
        return window;
    }
    public void setWindow(boolean window) {
        this.window = window;
    }
    public boolean isAisle() {
        return aisle;
    }
    public void setAisle(boolean aisle) {
        this.aisle = aisle;
    }

    public boolean isTable() {
        return table;
    }
    public void setTable(boolean table) {
        this.table = table;
    }
    public String getF() {
        return f;
    }
    public void setF(String f) {
        this.f = f;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
    public boolean isEase() {
        return ease;
    }
    public void setEase(boolean ease) {
        this.ease = ease;
    }
}

public class Driver {
    static Scanner S = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {

        Scanner inFile = new Scanner(new File("//Users//Mike//Desktop//Seats-2.txt"));

        String reservation = inFile.nextLine();
        Seat seat [] = new Seat [43];


        //while (inFile.hasNextLine()){
            //for(int i = 0; i <= reservation.length(); i++){
                //System.out.println(reservation.toString(seat));
            //}
        //}

我已经尝试过equals(reservation.toString())之类的方法,但是由于从Class Seat构建的数组,这些方法无法工作。 任何指导都会非常有用。

我不是在寻找简单的解决方法,而只是寻找一些关于在哪里寻找的指导。 谢谢

1 个答案:

答案 0 :(得分:0)

如果文本文件很小,请让它在字符串

中完整阅读
public static String ReadWholeFile(String filename) throws IOException
{
    final File file = new File(filename);
    final FileInputStream fis = new FileInputStream(file);
    final byte[] data = new byte[(int)file.length()];
    fis.read(data);
    fis.close();
    return new String(data, "UTF-8");
}

然后逐行解析,在Seats转换

public List<Seat> getSeats(String filename) throws IOException {
    final String[] lines = ReadWholeFile(filename).split("\n");

    final List<Seat> ret = new ArrayList<Seat>();

    for (int i=0; i<lines.length; i++) 
        try {
            final String[] parts = lines[i].split("\\s"); // split on whitespaces
            final int num = Integer.parseInt(parts[0]);

            ret.add(new Seat(num, parts[1], isTrue(parts[2]), isTrue(parts[3]), isTrue(parts[4]), isTrue(parts[5]), isTrue(parts[6]));
        }
        catch (Exception e) { /* whatever */ }

    return ret;
}

表示真实的字符串是&#34; T&#34;,&#34; true&#34;,...?如果是这样的话:

public static isTrue(String x) {
    return x.startsWith("T") || x.startsWith("t");
}

如果您真的不想阅读整个文件,可以选择:

public static List<Seat> getSeats2(String filename) {

    final List<Seat> ret = new ArrayList<Seat>();
    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(filename));

        String line;
        while (line = br.readLine()) {
            final String[] parts = line.split("\\s"); // split on whitespaces
            // as above
        }
    }
    catch (Exception e) { /* handle errors */ }
    finally {
        if (br != null)
            try { br.close(); }
            catch (Exception e) {}
    }

    return res;
}