Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class PokedexDriverCSV {
- public static void main(String[] args) {
- Pokedex pokedex = new Pokedex();
- // The following is adapted from Example 1. Using Buffered Reader and String.split() from https://www.javainterviewpoint.com/how-to-read-and-parse-csv-file-in-java/
- BufferedReader br = null;
- try
- {
- //Reading the csv file
- br = new BufferedReader(new FileReader("Pokemon.csv"));
- //Create List for holding Pokemon objects
- List<Pokemon> pokemonList = new ArrayList<Pokemon>();
- String line = "";
- //Read to skip the header
- br.readLine();
- //Reading from the second line
- while ((line = br.readLine()) != null)
- {
- String[] pokemonDetails = line.split(",");
- if(pokemonDetails.length > 0 )
- {
- //Create a temporary pokemon
- Pokemon pokemon = new Pokemon(Integer.parseInt(pokemonDetails[0]),
- pokemonDetails[1],pokemonDetails[2]);
- //Save the Pokemon details in Pokemon object
- pokemonList.add(pokemon);
- pokedex.addPokemon(pokemon);
- }
- }
- System.out.println("############ POKEDEX CONTENTS ############");
- System.out.println(pokedex);
- }
- catch(Exception ee)
- {
- ee.printStackTrace();
- }
- finally
- {
- try
- {
- br.close();
- }
- catch(IOException ie)
- {
- System.out.println("Error occured while closing the BufferedReader");
- ie.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement