Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will take a sentence (a single String), picks out the indiv

ID: 3830966 • Letter: W

Question

Write a program that will take a sentence (a single String), picks out the individual words (substrings), and puts each of them into a String array. Ex. input: "the lazy dog" output:the

                                                                                              lazy

                                                                                              dog

keep in mind that I am a beginner, so don't make it to difficult for me to undesrstand please. The teacher also said to use indexOf. Also remember to put every individual word into an array before it is spit back out. Thank you.

Explanation / Answer

StringSplit.java

import java.util.Scanner;


public class StringSplit {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the sentence: ");
       String s = scan.nextLine();
       String words[] = s.split("\s+");
       for(String word: words){
           System.out.println(word);
       }
   }

}

Output:

Enter the sentence:
the lazy dog
the
lazy
dog

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote