Guest User

Untitled

a guest
Jan 24th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public static void reader(String fileName) throws IOException {
  2. try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  3. String line;
  4. List<String> lineHolder = new ArrayList<>();
  5. StringTokenizer divider;
  6. String formatter = "%2s|%-30s|%-30s|%10s|%n";
  7.  
  8. String separator = "----------";
  9. System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
  10. System.out.print("--+" + separator + separator + separator
  11. + "+" + separator + separator + separator + "+"
  12. + separator + "+n");
  13.  
  14. while ((line = br.readLine()) != null){
  15. if (line.startsWith("#", 0))
  16. continue;
  17. if (!line.contains(";")) {
  18. throw new IOException("too less data or not proper delimiter");
  19. }
  20. divider = new StringTokenizer(line, ";");
  21. lineHolder = arrayFiller(divider);
  22. dataChecker(lineHolder, line);
  23. System.out.format(formatter, lineHolder.get(0), lineHolder.get(1)
  24. , lineHolder.get(2), lineHolder.get(3));
  25. }
  26. } catch (FileNotFoundException ex) {
  27. System.err.println("The file not found.");
  28. } catch (IOException ex){
  29. System.err.println(ex.getMessage());
  30. }
  31. System.out.print("n");
  32. }
  33.  
  34. public static ArrayList<String> arrayFiller(StringTokenizer divider) {
  35. ArrayList<String> lineHolder = new ArrayList<>();
  36. while (divider.hasMoreTokens()) {
  37. lineHolder.add(divider.nextToken());
  38. }
  39. return lineHolder;
  40. }
  41.  
  42. public static void dataChecker(List<String> lineHolder, String line) throws IOException {
  43. if (lineHolder.size() < 4) {
  44. throw new IOException("too less data or not proper delimiter");
  45. } else if (lineHolder.size() > 4) {
  46. throw new IOException("too much data");
  47. } else if (lineHolder.get(0).length() > 2
  48. || !Pattern.matches("[0-9]+", lineHolder.get(0))) {
  49. throw new IOException("Error during reading the file: "
  50. + "not proper ID field format");
  51. } else if (lineHolder.get(1).length() > 30
  52. || !Pattern.matches("[a-zA-ZíÍöÖüÜóÓőŐúÚűŰáÁéÉ. ]+", lineHolder.get(1))) {
  53. throw new IOException("Error during reading the file: "
  54. + "not proper Name field format");
  55. } else if (lineHolder.get(2).length() > 30
  56. || !Pattern.matches("[a-zA-Z0-9@. ]+", lineHolder.get(2))) {
  57. throw new IOException("Error during reading the file: "
  58. + "not proper Email field format");
  59. } else if (lineHolder.get(3).length() > 10 || dateFormatter(lineHolder.get(3))) {
  60. throw new IOException("Error during reading the file: "
  61. + "not proper Birth date field format");
  62. }
  63. }
  64.  
  65. public static boolean dateFormatter(String datum) {
  66. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  67. try {
  68. LocalDate changedDate = LocalDate.parse(datum, dtf);
  69. return false;
  70. } catch (DateTimeParseException ex) {
  71. return true;
  72. }
  73. }
  74.  
  75. #ID;name;email;birth date
  76. 1,Jakob George,gipszjakab@gmail.com,1981-11-23
  77. 2;Susan Smith;usa@gmail.com;1982-12-01
  78. 3;Poul Drake;gameover@gmail.com;1990-01-02
  79. 4;Isaac Wheather;ruck%sack@freemail.hu;1988-01-22
  80. 5;George T. Benson;bigman@hotmail.com;1977-08-12
Add Comment
Please, Sign In to add comment