Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        String input = "this is a test";
        StringBuilder result = new StringBuilder();
        char currentCharacter;
        int count;

        for (int i = 0; i < input.length(); i++) {
            currentCharacter = input.charAt(i);
            count = 1;
            while (i < input.length() - 1 && input.charAt(i + 1) == currentCharacter) {
                count++;
                i++;
            }
            result.append(currentCharacter);
            result.append(count);
        }

        System.out.println("" + result);
    }
}