So I am fairly new to Java and I have been trying to create a simple program that reads a list of names and a number from an external text file.

So I will show you that code first.

public static void startReadFile()
        // Get an ArrayList of accounts according to what is written in a file
        ArrayList<PokemonTrainer> trainerList = readFile();
        // To prove we successfully read the file, print out details of the accounts

     * Returns an ArrayList of accounts whose owners and balances will
     * depend on the values found in a file
    public static ArrayList<PokemonTrainer> readFile()
        ArrayList<PokemonTrainer> trainerList = new ArrayList<PokemonTrainer>();

         // Build a Scanner object to read user input
            Scanner s = new Scanner(System.in);
            // Tell the user what to do
            System.out.print("Please enter the filename containing the list: ");
        // Prepare to read from file
        String filename = s.nextLine();
        File file = new File(filename);
        Scanner in = new Scanner(file);
        // Make an ArrayList to store all the accounts we will make

        // Read each line until end of file is reached
        while (in.hasNextLine())
            // Read an entire line, which contains all the details for 1 account
            String trainerName = in.next();

            int amountOfPokemon = in.nextInt();
            // Create an Account belonging to the owner we found in the file
            PokemonTrainer pt = new PokemonTrainer(trainerName);
            // Put money into the account according to the amount of money we found in the file
            // Put the Account into the ArrayList
        catch (FileNotFoundException e)
            System.out.println("The file could not be opened, please try again");
        // Return the ArrayList containing all the accounts we made
        return trainerList;

     * Prints the owners and balances of all accounts in the given ArrayList
    public static void printTrainers(ArrayList<PokemonTrainer> trainerList)
        if (trainerList.isEmpty())
            System.out.println("There is nothing to print.");

        // Loop thru each account
        for (int i=0; i<trainerList.size(); i++)
            // Get the current account
            PokemonTrainer pt = trainerList.get(i);

            // Print the owner and balance          
            System.out.println(pt.getTrainerName() + " has " + pt.getPokemon() + " Pokemon");
        // Print a blank line to make it easier to see the results

Ignore some of those comments I based my code on another program I created earlier, so basically what is happening is a menu appears, when the user chooses the 'Read from text file' optio the user inputs the filename, the file is read and the contents inserted into an arrayList, once this is done the result is printed. Now this all works fine, but another option is to display the current list, this is where I cannot get it working, I try using the same printTrainers method but I get a 'cannot be applied error'.

If you see there after I print the code after adding it, I have a updateList(trainerList) in hopes that I would be able to print the updateList() but this is where I am getting the 'cannot be applied' error which makes sense, so I am not sure how to ONLY access the updated arrayList.

public static ArrayList<PokemonTrainer> createArrayList()
        System.out.println("You have sucessfully created an empty list, all previous data has been deleted.");
        return trainerList;
    public static ArrayList<PokemonTrainer> updateList(ArrayList<PokemonTrainer> updatedList)
        ArrayList<PokemonTrainer> trainerList = updatedList;
        return trainerList;

This is my initial code, the first method is used to create a blank list whenever the user chooses that option from the menu. If required I can post my full code but it's a hump of mess right now because I have been trying so many different things. I know this is a simple thing that I am overlooking but once you spend too much time on something you loose your sense of logic I think.

Thank you in advance!

6 Years
Discussion Span
Last Post by quuba

First part of code:
In method (line 1) locally declared trainerList-varible is only visible inside this method.

Second part of code:
Method in line 1 method createArrayList() should return return new ArrayList<PokemonTrainer>();
Show your static fields, and original compiler messages

Edited by quuba: add


Hi quuba, thank you for the reply!

I have fixed my problem since posting this question. My code went under a lot of modification since this post, so I'll try and explain it my best.

What I had to do was parse my actual list 'trainerList' to the listMenu() method, like this listMenu(trainerList) that way each option was able to take the list and do what it needed to do it with whilst updating it.

Slow steps :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.