0Pricing
Java Academy · Lesson

2D Data Cleaning & Validation

Clean and validate 2D integer data: guard null rows, bounds-check indices, clamp out-of-range values, normalize jagged shapes, and produce validation reports.

2D Data Cleaning & Validation is a free Java Academy lesson on CoddyKit — lesson 6 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Clean 2D?

Goal: Make 2D integer data safe and consistent. We will: (1) guard null rows, (2) verify bounds per row, (3) clamp values into a valid range, (4) normalize jagged shapes when needed, and (5) produce a validation report.

Nulls & Bounds

Step 1: Replace null rows with empty arrays and copy values defensively. Provide an inBounds helper to guard per-row lengths.

public class Main {
  // Make a defensive copy of m. Replace null rows with empty arrays.
  static int[][] copyOrEmpty(int[][] m) {
    if (m == null) return new int[0][0];
    int[][] out = new int[m.length][];
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r] == null) {
        out[r] = new int[0];
      } else {
        out[r] = new int[m[r].length];
        for (int c = 0; c < m[r].length; c = c + 1) out[r][c] = m[r][c];
      }
    }
    return out;
  }

  // Safe bounds check for jagged matrices
  static boolean inBounds(int[][] m, int r, int c) {
    return m != null && r >= 0 && r < m.length && m[r] != null && c >= 0 && c < m[r].length;
  }

  public static void main(String[] args) {
    int[][] raw = new int[][] { null, {1, 2, 3}, {4} };
    int[][] cleaned = copyOrEmpty(raw);

    System.out.println("rows = " + cleaned.length);
    for (int r = 0; r < cleaned.length; r = r + 1) System.out.println("row " + r + " len = " + cleaned[r].length);

    System.out.println("inBounds(1,2) = " + inBounds(cleaned, 1, 2));  // true
    System.out.println("inBounds(2,2) = " + inBounds(cleaned, 2, 2));  // false
  }
}

Clamp Values

Step 2: Clamp values into a valid range (e.g., 0–100). Invalid negatives become 0, too-large values become 100.

public class Main {
  static int clamp(int v, int lo, int hi) { if (v < lo) return lo; if (v > hi) return hi; return v; }
  static void clampAll(int[][] m, int lo, int hi) {
    if (m == null) return;
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r] == null) continue;
      for (int c = 0; c < m[r].length; c = c + 1) m[r][c] = clamp(m[r][c], lo, hi);
    }
  }
  static void print(int[][] m) {
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r] == null) { System.out.println("<null row>"); continue; }
      for (int c = 0; c < m[r].length; c = c + 1) System.out.print(m[r][c] + (c + 1 < m[r].length ? " " : ""));
      System.out.println();
    }
  }
  public static void main(String[] args) {
    int[][] m = { { -5, 101, 50 }, null, { 200 } };
    System.out.println("before clamp:");
    print(m);
    clampAll(m, 0, 100);
    System.out.println("after clamp [0..100]:");
    print(m);
  }
}

Normalize Shape

Step 3: Normalize to a rectangular shape by padding each short (or null) row up to the longest row length using a pad value (e.g., 0).

public class Main {
  static int[][] padToMaxCols(int[][] m, int pad) {
    if (m == null) return new int[0][0];
    int rows = m.length;
    int maxCols = 0;
    for (int r = 0; r < rows; r = r + 1) if (m[r] != null && m[r].length > maxCols) maxCols = m[r].length;
    int[][] out = new int[rows][maxCols];
    for (int r = 0; r < rows; r = r + 1) {
      int cols = (m[r] == null) ? 0 : m[r].length;
      for (int c = 0; c < cols; c = c + 1) out[r][c] = m[r][c];
      for (int c = cols; c < maxCols; c = c + 1) out[r][c] = pad;  // pad missing cells
    }
    return out;
  }
  static void print(int[][] x) {
    for (int r = 0; r < x.length; r = r + 1) {
      for (int c = 0; c < x[r].length; c = c + 1) System.out.print(x[r][c] + (c + 1 < x[r].length ? " " : ""));
      System.out.println();
    }
  }
  public static void main(String[] args) {
    int[][] jag = { {1, 23, 456}, {7, 8}, null, {90} };
    int[][] rect = padToMaxCols(jag, 0);
    print(rect);
  }
}

Validation Report

Step 4: Build a validation report: count negative values, too-large values, null rows and total visited cells.

public class Main {
  static class Report { int negatives; int overMax; int nullRows; int cells; }
  static Report validate(int[][] m, int hi) {
    Report r = new Report();
    if (m == null) return r;
    for (int i = 0; i < m.length; i = i + 1) {
      if (m[i] == null) { r.nullRows = r.nullRows + 1; continue; }
      for (int j = 0; j < m[i].length; j = j + 1) {
        int v = m[i][j];
        r.cells = r.cells + 1;
        if (v < 0) r.negatives = r.negatives + 1;
        if (v > hi) r.overMax = r.overMax + 1;
      }
    }
    return r;
  }
  public static void main(String[] args) {
    int[][] m = { { -5, 101, 50 }, null, { 200 } };
    Report r = validate(m, 100);
    System.out.println("cells=" + r.cells + ", negatives=" + r.negatives + ", overMax=" + r.overMax + ", nullRows=" + r.nullRows);
  }
}

Skip-Null Print

Step 5: Provide aligned printing that gracefully skips null rows and respects each row's length.

public class Main {
  static boolean inBounds(int[][] m, int r, int c) {
    return m != null && r >= 0 && r < m.length && m[r] != null && c >= 0 && c < m[r].length;
  }
  static int[] widths(int[][] m) {
    int maxCols = 0; if (m == null) return new int[0];
    for (int r = 0; r < m.length; r = r + 1) if (m[r] != null && m[r].length > maxCols) maxCols = m[r].length;
    int[] w = new int[maxCols];
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r] == null) continue;
      for (int c = 0; c < m[r].length; c = c + 1) {
        int len = Integer.toString(m[r][c]).length();
        if (len > w[c]) w[c] = len;
      }
    }
    for (int c = 0; c < w.length; c = c + 1) if (w[c] < 1) w[c] = 1;
    return w;
  }
  static void printAlignedSkipNull(int[][] m) {
    int[] w = widths(m);
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r] == null) { System.out.println("<skipped null row>"); continue; }
      String line = "";
      for (int c = 0; c < m[r].length; c = c + 1) {
        String s = Integer.toString(m[r][c]);
        while (s.length() < w[c]) s = " " + s;
        line = line + s + (c + 1 < m[r].length ? " " : "");
      }
      System.out.println(line);
    }
  }
  public static void main(String[] args) {
    int[][] m = { { 1, 200, -3 }, null, { 45 } };
    printAlignedSkipNull(m);
  }
}

Safe Access Check

Quick check: How do you safely access m[r][c] on potentially jagged data?

Recap & Next

Recap: You copied data defensively, guarded indices, clamped values, normalized jagged shapes, and generated validation summaries. These building blocks make later 2D algorithms robust.

Frequently asked questions

Is the “2D Data Cleaning & Validation” lesson free?

Yes — the full text of “2D Data Cleaning & Validation” is free to read here on the web, and the Java Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “2D Data Cleaning & Validation”?

Clean and validate 2D integer data: guard null rows, bounds-check indices, clamp out-of-range values, normalize jagged shapes, and produce validation reports. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 6 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “2D Data Cleaning & Validation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. 2D Array Basics & Nested Iteration
  2. Jagged Arrays & Table Formatting
  3. Row/Column Maxima, Transpose & Formatting
  4. 2D Mini-Project: Gradebook
  5. 2D Utilities as a Helper Class (Refactor)
  6. 2D Data Cleaning & Validation
← Back to Java Academy