Menu
Coddy logo textTech

Word Processing

Part of the Logic & Flow section of Coddy's Java journey — lesson 51 of 59.

Now we'll process each word by removing punctuation and converting to lowercase. This will help us count words accurately regardless of their case or surrounding punctuation.

String word = "Hello!";
// Remove punctuation and convert to lowercase
word = word.replaceAll("[^a-zA-Z ]", "").toLowerCase();
System.out.println(word);
// prints: hello
challenge icon

Challenge

Easy

Modify the previous program to clean each word by removing punctuation and converting to lowercase. Print both the original and processed words.

For example

Input:

Coddy
Expected Output:
Original[0,0]: Coddy
Processed[0,0]: coddy

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        
        String[] sentences = text.split("\\.");
        String[][] textArray = new String[sentences.length][];
        
        for (int i = 0; i < sentences.length; i++) {
            textArray[i] = sentences[i].trim().split(" ");
        }
        
        for (int i = 0; i < textArray.length; i++) {
            for (int j = 0; j < textArray[i].length; j++) {
                if (!textArray[i][j].isEmpty()) {
                    System.out.println("Word[" + i + "," + j + "]: " + textArray[i][j]);
                }
            }
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow