Advertisement
Glorksnork

PokedexCSV

Jan 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class PokedexDriverCSV {
  9. public static void main(String[] args) {
  10. Pokedex pokedex = new Pokedex();
  11.  
  12. // 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/
  13. BufferedReader br = null;
  14. try
  15. {
  16. //Reading the csv file
  17. br = new BufferedReader(new FileReader("Pokemon.csv"));
  18.  
  19. //Create List for holding Pokemon objects
  20. List<Pokemon> pokemonList = new ArrayList<Pokemon>();
  21. String line = "";
  22. //Read to skip the header
  23. br.readLine();
  24. //Reading from the second line
  25. while ((line = br.readLine()) != null)
  26. {
  27. String[] pokemonDetails = line.split(",");
  28.  
  29.  
  30. if(pokemonDetails.length > 0 )
  31. {
  32. //Create a temporary pokemon
  33. Pokemon pokemon = new Pokemon(Integer.parseInt(pokemonDetails[0]),
  34. pokemonDetails[1],pokemonDetails[2]);
  35.  
  36. //Save the Pokemon details in Pokemon object
  37. pokemonList.add(pokemon);
  38.  
  39.  
  40.  
  41.  
  42. pokedex.addPokemon(pokemon);
  43. }
  44. }
  45.  
  46. System.out.println("############ POKEDEX CONTENTS ############");
  47. System.out.println(pokedex);
  48. }
  49. catch(Exception ee)
  50. {
  51. ee.printStackTrace();
  52. }
  53. finally
  54. {
  55. try
  56. {
  57. br.close();
  58. }
  59. catch(IOException ie)
  60. {
  61. System.out.println("Error occured while closing the BufferedReader");
  62. ie.printStackTrace();
  63. }
  64. }
  65.  
  66.  
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement