1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.io.StreamTokenizer;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class RecordErrorSurface {
  7. public static void surfaceErrors(String record, String delimiter) {
  8. // Use StreamTokenizer for parsing (compatible with older Java versions)
  9. StreamTokenizer tokenizer = new StreamTokenizer(new java.io.StringReader(record));
  10. tokenizer.quoteChar = '"'; // Handle quoted fields
  11. tokenizer.whitespace = true; // Treat whitespace as delimiters
  12. List<String> fields = new ArrayList<>();
  13. try {
  14. while (tokenizer.nextToken() != tokenizer.TT_EOF) {
  15. fields.add(tokenizer.sval); // Get field value
  16. }
  17. } catch (IOException e) {
  18. System.err.println("Error parsing record: " + e.getMessage());
  19. return; // Exit if parsing fails
  20. }
  21. // Check for errors (example: record too short)
  22. if (fields.size() < 2) {
  23. System.err.println("Error: Record contains less than 2 fields.");
  24. System.err.println("Record: " + record);
  25. return;
  26. }
  27. // Example: checking for null or empty fields
  28. for (int i = 0; i < fields.size(); i++) {
  29. if (fields.get(i) == null || fields.get(i).trim().isEmpty()) {
  30. System.err.println("Error: Field '" + (i + 1) + "' is null or empty.");
  31. System.err.println("Record: " + record);
  32. return;
  33. }
  34. }
  35. //More complex validation can be added here,
  36. //e.g., checking data types, ranges, etc.
  37. System.out.println("Record is valid.");
  38. }
  39. public static void main(String[] args) {
  40. //Example Usage
  41. String record1 = "John,Doe,30,New York";
  42. String record2 = "Jane , , 25, London"; //Missing data
  43. String record3 = "Peter,Smith"; //Too few fields
  44. String record4 = "Alice,\"Brown, Mary\",40,Paris"; //Quoted field
  45. System.out.println("Testing record: " + record1);
  46. surfaceErrors(record1, ",");
  47. System.out.println("\nTesting record: " + record2);
  48. surfaceErrors(record2, ",");
  49. System.out.println("\nTesting record: " + record3);
  50. surfaceErrors(record3, ",");
  51. System.out.println("\nTesting record: " + record4);
  52. surfaceErrors(record4, ",");
  53. }
  54. }

Add your comment