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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.