0Pricing
Java Academy · Lesson

Looping Over Characters

Process each char in a string.

Looping Over Characters is a free Java Academy lesson on CoddyKit — lesson 3 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Strings Are Made of Chars

A String is a sequence of characters. Each character sits at a position called an index, starting at 0.

In "Java", index 0 is 'J', index 1 is 'a', and so on. Looping lets you visit each char in order.

String word = "Java";
// indexes: 0=J 1=a 2=v 3=a

charAt Gets One Character

The charAt(index) method returns the char at a given position in a String.

Indexes run from 0 to length() - 1. Asking for an index outside that range throws an error.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        System.out.println(word.charAt(0));
        System.out.println(word.charAt(3));
    }
}

How Long Is the String?

The length() method returns how many characters a String has.

For "Java" it returns 4. Because indexes start at 0, the last valid index is always length() - 1.

public class Main {
    public static void main(String[] args) {
        String word = "Hello";
        System.out.println(word.length());
    }
}

Looping With an Index

A classic for loop runs the index from 0 up to length() - 1, calling charAt each time.

This gives you both the position and the character, which is useful when the index matters.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        for (int i = 0; i < word.length(); i++) {
            System.out.println(word.charAt(i));
        }
    }
}

The Enhanced for Loop

If you do not need the index, convert the String to a char array with toCharArray() and use a for-each loop.

This reads cleanly: for each char c in the word, do something.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        for (char c : word.toCharArray()) {
            System.out.println(c);
        }
    }
}

Printing on One Line

Use System.out.print instead of println to keep characters on the same line.

This is handy when rebuilding or transforming text character by character.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        for (char c : word.toCharArray()) {
            System.out.print(c);
        }
        System.out.println();
    }
}

Transforming Each Char

Inside the loop you can change each char before using it. Here we uppercase every letter as we go.

We build a new String because the original String cannot be modified; Strings are immutable.

public class Main {
    public static void main(String[] args) {
        String word = "java";
        String result = "";
        for (char c : word.toCharArray()) {
            result += Character.toUpperCase(c);
        }
        System.out.println(result);
    }
}

Looping Backwards

To reverse a String, loop from the last index down to 0 using charAt.

Start i at length() - 1 and decrease it until it reaches 0.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        for (int i = word.length() - 1; i >= 0; i--) {
            System.out.print(word.charAt(i));
        }
        System.out.println();
    }
}

Watch the Boundaries

Always loop while i < word.length(), never i <= word.length().

Going one past the end calls charAt with an invalid index and throws a StringIndexOutOfBoundsException.

String word = "Hi";
// valid indexes: 0 and 1 only
// charAt(2) would crash

Finding a Character

Loop and compare each char with == to find a target. Here we report the index of the first 'a'.

We stop early with break once we find it.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == 'a') {
                System.out.println("Found at " + i);
                break;
            }
        }
    }
}

Reading From a String

Looping over chars is the foundation of search, replace, and validation tasks.

Whenever you need to inspect text closely, walking through each char with a loop is the standard tool.

for (char c : "ab".toCharArray()) {
    System.out.println(c);
}

Quick Check

Test your understanding of looping over characters.

Recap

Use charAt(i) with an index loop, or toCharArray() with a for-each loop, to visit every char.

Loop while i < length() to stay in bounds, and build new Strings since Strings are immutable. This pattern powers searching and transforming text.

Frequently asked questions

Is the “Looping Over Characters” lesson free?

Yes — the full text of “Looping Over Characters” is free to read here on the web, and the Java Academy course includes 4 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 “Looping Over Characters”?

Process each char in a string. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping Over Characters” 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. The char Primitive
  2. Character Helper Methods
  3. Looping Over Characters
  4. Counting and Filtering Chars
← Back to Java Academy