for循环用空格分隔字符串(Java)

jer*_*246 -4 java string spaces

有没有办法创建某种类型的For循环来用空格分隔字符串?到目前为止,我可以显示一个字符串并查找它有多少个字符.

import java.util.Scanner;
import java.lang.String;

public class Word{
  public static void main(String args[])
  {
     Scanner scan = new Scanner(System.in);

     int b;

    String word;

    System.out.println ("Enter a word: ");
    word = scan.next();

    b = word.length();

    System.out.println (word);
    System.out.println (b);
  }
}
Run Code Online (Sandbox Code Playgroud)

hig*_*ted 7

作为替代方案Scanner,您可以执行以下操作:

String[] parts = line.split(" ");
for (String part : parts) {
    //do something interesting here
}
Run Code Online (Sandbox Code Playgroud)