Question

//I am trying to take in this file and separate the Cards based on the ######### at the end //I am making a robocop style card game in java for fun and I made the cards in plain text file and I want to be able to read in the file and separate the file into cards. Ultimately I want to be able to call these cards and print them out when needed.

try{

                File file = new File("MyText.txt");
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = null;
                while((line = reader.readLine()) != null){
                    addCard(line);
                }

            }catch(Exception ex){
                ex.printStackTrace();
            }
            //Problem! This only prints the first card?!? HELP! 
            for(i = 0; i < playerList.size(); i++){
                System.out.println(playerList.get(i));
            }

    }


    void addCard(String lineToParse){

        String[] tokens = lineToParse.split("##########");
        // How do I increment the token to the next token and add to playerList?
        playerList.add(tokens[0]);

    }
}
Was it helpful?

Solution

For your addCard method, you need:

void addCard(String lineToParse){

    String[] tokens = lineToParse.split("##########");
    for(int i=0; i<tokens.length; i++) {
        playerList.add(tokens[i]);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top