import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class DataCleaner {
private static final int MAX_RETRIES = 3;
private static final int CLEANING_ATTEMPTS = 3;
private static final Random random = new Random();
public static List<String> cleanData(String filePath) throws IOException {
List<String> cleanedData = new ArrayList<>();
int retryCount = 0;
while (retryCount < MAX_RETRIES) {
try {
cleanedData = cleanFile(filePath);
break; // Exit loop if cleaning is successful
} catch (IOException e) {
retryCount++;
if (retryCount < MAX_RETRIES) {
System.err.println("Error cleaning file. Retry attempt: " + retryCount);
try {
Thread.sleep(1000 * (random.nextDouble() * 3 + 1)); // Random delay between 1-3 seconds
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
} else {
System.err.println("Failed to clean file after multiple retries.");
throw e; // Re-throw the exception after max retries
}
}
}
return cleanedData;
}
private static List<String> cleanFile(String filePath) throws IOException {
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
line = cleanLine(line);
if (!line.isEmpty()) { //Only add non-empty lines
lines.add(line);
}
}
}
return lines;
}
private static String cleanLine(String line) {
// Remove leading/trailing whitespace
line = line.trim();
// Remove multiple spaces
line = line.replaceAll("\\s+", " ");
//Remove special characters (keep only alphanumeric and spaces)
line = line.replaceAll("[^a-zA-Z0-9\\s]", "");
//Remove extra spaces
line = line.trim();
//Remove empty strings
if(line.isEmpty()){
return "";
}
return line;
}
public static void main(String[] args) {
try {
// Example usage
List<String> data = cleanData("input.txt"); // Replace with your file path
for (String cleanedLine : data) {
System.out.println(cleanedLine);
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Add your comment