Java解析多个文件

4

我需要解析多个文件并且在对象初始化外部访问对象方法。以下是我的代码:

public static void main(String[] args) {

    try {
        File Attrationfile = new File("attractions.txt");
        Scanner attractionscanner = null;
        attractionscanner = new Scanner(Attrationfile);
        while (attractionscanner.hasNext()) {
        String nextline = attractionscanner.nextLine();
        String[] Attractioncomponents = nextline.split("@");

            String ridename =Attractioncomponents[0];
            int price = Integer.parseInt(Attractioncomponents[1]);
            String type = Attractioncomponents[2];
            int unknown = Integer.parseInt(Attractioncomponents[3]) ;
            double speed = Attractioncomponents.length <= 4 ? 0 :
            Double.parseDouble(Attractioncomponents[4]);

            RollerCoaster rollerCoaster = new RollerCoaster(ridename, price , unknown, speed);

        }

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

    try {
         File Customerfile = new File("customers.txt");
         Scanner  Customerscanner = new Scanner(Customerfile);

         while (Customerscanner.hasNext()) {
             String nextline = Customerscanner.nextLine();
             String[] Customercomponents = nextline.split("#");

             int accountnumber =Integer.parseInt(Customercomponents[0]);
             String name = Customercomponents[1];
             int age = Integer.parseInt(Customercomponents[2]) ;
             int balance = Integer.parseInt(Customercomponents[3]) ;
             String discount = Customercomponents.length <= 4
                     ? String.valueOf(0) : Customercomponents[4];
             Customer customer= new Customer(accountnumber,name, age, balance, discount);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

这个代码可以运行,但是我无法在它们的循环之外访问对象。我不确定如何使“顾客”类获取有关过山车的信息,例如名称和价格。例如,如果顾客和过山车对象在同一区域,那么我就能通过从rollercoaster.getprice减去customer.getbalance来更新顾客余额,并将customer.setbalance设置为计算值。正如你可能已经了解到的那样,我是一个初学者,所以我可能走了错误的路 - 谢谢。

1
在循环之前声明你的关键对象,通常是一个集合或一些诸如ArrayList之类的对象。 - Hovercraft Full Of Eels
3个回答

1
你可以通过在主方法的开头声明这些变量来改变它们的作用范围。
public static void main(String[] args) {
        Customer customer = null;
        RollerCoaster rollerCoaster = null;

        try {
            File Attrationfile = new File("attractions.txt");
            Scanner attractionscanner = null;
            attractionscanner = new Scanner(Attrationfile);
            while (attractionscanner.hasNext()) {
            String nextline = attractionscanner.nextLine();
            String[] Attractioncomponents = nextline.split("@");

                String ridename =Attractioncomponents[0];
                int price = Integer.parseInt(Attractioncomponents[1]);
                String type = Attractioncomponents[2];
                int unknown = Integer.parseInt(Attractioncomponents[3]) ;
                double speed = Attractioncomponents.length <= 4 ? 0 :
                Double.parseDouble(Attractioncomponents[4]);

                rollerCoaster = new RollerCoaster(ridename, price , unknown, speed);

            }

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

        try {
             File Customerfile = new File("customers.txt");
             Scanner  Customerscanner = new Scanner(Customerfile);
             while (Customerscanner.hasNext()) {
             String nextline = Customerscanner.nextLine();
             String[] Customercomponents = nextline.split("#");

                int accountnumber =Integer.parseInt(Customercomponents[0]);
                String name = Customercomponents[1];
                int age = Integer.parseInt(Customercomponents[2]) ;
                int balance = Integer.parseInt(Customercomponents[3]) ;
                String discount = Customercomponents.length <= 4 ? String.valueOf(0) :
                        Customercomponents[4];
                customer= new Customer(accountnumber,name , age , balance, discount);


    }

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

每次读取新行时,此解决方案将重新初始化customerrollerCoaster。这实际上意味着您只能获得一个具有文件最后一行的值的对象。据我所知,OP想要的是在文件中定义的事物列表。如果OP确实只想要最后一个对象,则在每次读取行时解析字符串并构造对象在性能方面是次优的。 - mindoverflow

1

欢迎来到SO!正如Hovercraft指出的那样,这些对象在循环范围内声明,意味着您无法在外部访问它们,正如您所注意到的那样。此外,它们在每次迭代中都被覆盖,因为您在每次通过时声明和初始化对象。考虑使用ArrayList,如下所示(仅供参考):

ArrayList<Customer> customerList = new ArrayList<>();
try {
    while (customerScanner.hasNext()) {
        // ...
        customerList.add(new Customer(accountnumber,name, age, balance, discount));
    }
} catch (...) {
    // ...
}

这里是关于ArrayList的文档。

<T>泛型类型,这意味着你可以有一个ArrayList<Customer>ArrayList<RollerCoaster>ArrayList<String>等等。

附注:按照惯例,变量名以小写字母开头,如Scanner customerScanner而不是Scanner Customerscanner


0

是作用域的问题吗?尝试在循环体外声明对象。因为在Java中,花括号是一个作用域。嵌套的花括号越多,作用域就越小。您可以尝试在相同或更大的作用域中声明需要调用的对象。

    String type = null;
    RollerCoaster rollerCoaster = null;
    while (attractionscanner.hasNext()) {
        String nextline = attractionscanner.nextLine();
        String[] Attractioncomponents = nextline.split("@");
        String ridename =Attractioncomponents[0];
        int price = Integer.parseInt(Attractioncomponents[1]);
        type = Attractioncomponents[2];
        int unknown = Integer.parseInt(Attractioncomponents[3]) ;
        double speed = Attractioncomponents.length <= 4 ? 0 :
        Double.parseDouble(Attractioncomponents[4]);
        rollerCoaster = new RollerCoaster(ridename, price , unknown, speed);

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接