Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 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.  
  6.  
  7. public class DominoCSVtoArrayList {
  8. public static void main(String[] args) {
  9.  
  10. BufferedReader dominoBuffer = null;
  11.  
  12. try {
  13. String dominoLine;
  14. dominoBuffer = new BufferedReader(new FileReader("src/dominos.csv")); //li le fichier CSV
  15.  
  16. while ((dominoLine = dominoBuffer.readLine())!=null) { //tant que chaque ligne lue n'est pas egale à null, on lit le fichier
  17. System.out.println ("RawCSV : "+ dominoLine); // et on renvoie cela.
  18. System.out.println("Converted ArrayList Data : "+ dominoCSVtoArrayList(dominoLine)+"\n"); //Insert a newline in the text at this point.
  19.  
  20. }
  21.  
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. if (dominoBuffer != null) dominoBuffer.close();
  27. } catch (IOException dominoException) {
  28. dominoException.printStackTrace();
  29.  
  30. }
  31. }
  32.  
  33. }
  34.  
  35. public static ArrayList<String> dominoCSVtoArrayList (String dominoCSV){
  36. ArrayList<String> dominoResult = new ArrayList <String>(); //nous créons une liste appelee dominoResult
  37.  
  38. if (dominoCSV != null) {
  39. String[] splitData = dominoCSV.split("\\s*,\\s*"); //si le document csv est différent de null, une virgule est utilisée pour séparer les éléments
  40. /*
  41. \s* any number of whitespace characters
  42. a comma
  43. \s* any number of whitespace characters
  44. which will split on commas and consume any spaces either side
  45.  
  46. */
  47. for (int i=0; i<splitData.length; i++) {
  48. if (!(splitData[i]== null) || !(splitData[i].length()==0)){
  49. dominoResult.add(splitData[i].trim()); // The java lang.string.trim()is a built-in function that eliminates leading and trailing spaces
  50.  
  51. }
  52. }
  53. }
  54. return dominoResult;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement