我需要解析多个文件并访问对象的初始化位置之外的方法。
这是我的代码:

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();
    }
}

这有效,但我无法访问循环之外的对象。我不确定 Сustomer 类将如何获取有关过山车的信息,例如名称和价格。例如,如果客户和过山车对象在同一区域,我将能够通过从 rollercoaster.getprice 中移除 customer.getbalance 并将 customer.setbalance 设置为计算值来更新客户余额。正如您可能已经收集到的那样,我是一个初学者,所以我可能以错误的方式来解决这个问题 - 谢谢。

最佳答案

您可以通过在 main 方法的开头声明它们来更改这些变量的范围。

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();
        }
    }

关于Java解析多个文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60707437/

10-09 04:53