Menu
Coddy logo textTech

Text Input and Storage

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

challenge icon

Challenge

Easy

Create a program that reads text from the user and stores it in a 2D array. The text may contain multiple sentences separated by periods. Print each word in the format "Word[row,col]: word".

To store words in a 2D array, we need to split each sentence into words:

String[][] textArray = new String[sentences.length][];
for (int i = 0; i < sentences.length; i++) {
   textArray[i] = sentences[i].trim().split(" ");
}

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][];
        
        // Write your code here
    }
}

All lessons in Logic & Flow