Java parsing multiple files

Viewed 333

I need to parse multiple files and get access to the object's methods outside of where they were initialized. This is my code:

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

This works but I can't get access to the objects outside of their loops. I am not sure how the Сustomer class would get information about the roller coaster, such as the name and price. For example, if the customer and rollercoaster objects were in the same area, I would be able to update the customer balance by taking away rollercoaster.getprice from the customer.getbalance, and setting customer.setbalance to the value of the calculation. As you have probably already gathered, I am a beginner, so I am probably going about this in the wrong way - thanks.

3 Answers

You can change the scope for those variables by declaring them at the start of the main method.

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

Welcome to SO! As Hovercraft pointed out, the objects are declared within the scope of the loop, meaning you can't access them outside of it as you noticed. Also, they are overwritten on every iteration, since you declare and initialize the object on every pass. Consider using an ArrayList like so (here just for the customers):

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

Here's the doc for the ArrayList.

<T> is a generic type, which for you means that you can have an ArrayList<Customer>, ArrayList<RollerCoaster>, ArrayList<String> ...

Sidenote: By convention, variable names start with a lowercase letter, like Scanner customerScanner instead of Scanner Customerscanner.

Is it a question of scope? Try to declare an object outside the body of the loop. Because in Java, the brace is a scope. The more nested braces, the smaller the scope. You can try to declare the objects you need to call in the external scope in the same or larger scope

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

    }
Related