1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5. public class DataCleaner {
  6. private static final int MAX_RETRIES = 3;
  7. private static final int CLEANING_ATTEMPTS = 3;
  8. private static final Random random = new Random();
  9. public static List<String> cleanData(String filePath) throws IOException {
  10. List<String> cleanedData = new ArrayList<>();
  11. int retryCount = 0;
  12. while (retryCount < MAX_RETRIES) {
  13. try {
  14. cleanedData = cleanFile(filePath);
  15. break; // Exit loop if cleaning is successful
  16. } catch (IOException e) {
  17. retryCount++;
  18. if (retryCount < MAX_RETRIES) {
  19. System.err.println("Error cleaning file. Retry attempt: " + retryCount);
  20. try {
  21. Thread.sleep(1000 * (random.nextDouble() * 3 + 1)); // Random delay between 1-3 seconds
  22. } catch (InterruptedException ie) {
  23. Thread.currentThread().interrupt();
  24. }
  25. } else {
  26. System.err.println("Failed to clean file after multiple retries.");
  27. throw e; // Re-throw the exception after max retries
  28. }
  29. }
  30. }
  31. return cleanedData;
  32. }
  33. private static List<String> cleanFile(String filePath) throws IOException {
  34. List<String> lines = new ArrayList<>();
  35. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  36. String line;
  37. while ((line = reader.readLine()) != null) {
  38. line = cleanLine(line);
  39. if (!line.isEmpty()) { //Only add non-empty lines
  40. lines.add(line);
  41. }
  42. }
  43. }
  44. return lines;
  45. }
  46. private static String cleanLine(String line) {
  47. // Remove leading/trailing whitespace
  48. line = line.trim();
  49. // Remove multiple spaces
  50. line = line.replaceAll("\\s+", " ");
  51. //Remove special characters (keep only alphanumeric and spaces)
  52. line = line.replaceAll("[^a-zA-Z0-9\\s]", "");
  53. //Remove extra spaces
  54. line = line.trim();
  55. //Remove empty strings
  56. if(line.isEmpty()){
  57. return "";
  58. }
  59. return line;
  60. }
  61. public static void main(String[] args) {
  62. try {
  63. // Example usage
  64. List<String> data = cleanData("input.txt"); // Replace with your file path
  65. for (String cleanedLine : data) {
  66. System.out.println(cleanedLine);
  67. }
  68. } catch (IOException e) {
  69. System.err.println("Error: " + e.getMessage());
  70. }
  71. }
  72. }

Add your comment