Text Input and Storage
Part of the Logic & Flow section of Coddy's Java journey — lesson 50 of 59.
Challenge
EasyCreate 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
1Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays2HashMap Part 1
What is a HashMap?Declare a HashMapAccessing ValuesCheck If Key ExistsModifying DictionariesRecap - HashMap