从数据文件中读取以填充 ArrayList [英] Reading from data file to fill ArrayList

查看:27
本文介绍了从数据文件中读取以填充 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有抽象类Orders"的程序,其中包含三个不同的类来实现它,当我使用硬编码订单对其进行测试时,程序中的所有内容都运行良好.

I have a program with an abstract class 'Orders' with three different classes implementing it everything in the program runs fine when I tested it with hard coded orders.

这里是抽象类:

public abstract class Order {
protected String location;
protected double price;

public Order(double price, String location){
    this.price = price;
    this.location = location;
}

public abstract double calculateBill();

public String getLocation() {
    return location;
}

public double getPrice() {
    return price;
}

public abstract String printOrder(String format);

}

这里是其中一个实现类供参考

And here is one of the implementing classes for reference

public class NonProfitOrder extends Order {

public NonProfitOrder(double price, String location) {
    super(price, location);
}

public double calculateBill() {
    return getPrice();
}

public String printOrder(String format){
    String Long = "Non-Profit Order" + "\nLocation: " + getLocation() +  "\nTotal Price: " + getPrice();
    String Short = "Non-Profit Order-Location: " + getLocation() + ", " + "Total Price: " + getPrice();
    if (format.equals("Long")){
        return Long;
    }
    else{
        return Short;
    }
}
}

到目前为止,这是我对测试人员的看法,我知道这是错误的并且相当混乱,但请放轻松.我一直在努力让一些东西工作,但没有运气.

This is what I have for a tester so far, I know it is wrong and pretty messy, but take it easy. Iv'e been messing around trying to get something to work, but no luck.

public static ArrayList<Order> readOrders (String fileName) throws FileNotFoundException{
    String type;
    Scanner s = new Scanner(new File("orders.txt"));
    ArrayList<Order> orders = new ArrayList<Order>();
    while (s.hasNext()){
        type = s.nextLine();
    }
    switch(type) {
        case 1: type = NonProfitOrder();
            break;
        case 2: type = RegularOrder();
            break;
        case 3: type = OverseasOrder();
    return orders;
}

}

我需要读取的数据文件如下所示:

The data file I need to read from looks like this:

N 1000.0 NY
R 2000.0 CA 0.09
R 500.0 GA 0.07
N 2000.0 WY
O 3000.0 Japan 0.11 20.0
N 555.50 CA
O 3300.0 Ecuador 0.03 30.0
R 600.0 NC 0.06

首先我在读取文件时遇到问题,我知道我需要一个循环来将数据添加到 arrayList,但我不确定如何 .在一种方法中读取和循环的最简单方法是什么?如果可能的话.

First off I am having trouble reading the file, I know i need a loop to add the data to the arrayList, but I'm not sure how . What would be the easiest way to read and loop all in one method? If possible.

我更新添加了一些我拥有的 switch 语句,但它不起作用.而不是案例 1,2,3,我是否需要使用 N、O、R 来匹配文件我在修复错误类型不匹配"时遇到问题.

I updated to add the somewhat of a switch statement that I have, that doesn't work. Instead of case 1,2,3, would I instead need to use something with N, O, R to match up with the file and I'm having trouble fixing the error "type mismatch".

推荐答案

扫描仪 提供了一种浏览文件的简便方法..next() .hasNext() .nextLine() .hasNextLine() 在您的情况下是非常有用的方法.

Scanner provides an easy way to walk through the file. .next() .hasNext() .nextLine() .hasNextLine() are very useful methods in your case.

这篇关于从数据文件中读取以填充 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆