我想将一个由 4 个单词组成的句子分解成单个单词。但是,我不知道如何告诉它从第一个空格之后开始,停止和句子中的下一个空格。
import java.util.Scanner;
public class arithmetic {
public static void main(String[] args) {
Scanner in = new Scanner(System.in)
String sentence;
String word1, word2, word3, word4;
int p, p2;
System.out.print("Enter a sentence with 4 words:");
sentence = in.nextLine();
p = sentence.indexOf(" ");
word1 = sentence.substring(0,p)+(" ");
word2 = sentence.substring()(" ");
word3 = sentence.substring()+(" ");
word4 = sentence.substring()+(" ");
sentence = word1+word2+word3+word4;
System.out.println(sentence);