I think the question is already answered.
(我认为这个问题已经回答。)
But instead of walking through the characters of the string it is better to use the standard Java library as follows: (但是,与其遍历字符串的字符,不如使用标准的Java库,如下所示:)
name = in.readLine();
if (name != null && !"".equals(name)) {
String[] arr = name.split("\s+");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
The split()
method does what you're trying to program yourself.
(split()
方法可以完成您要编程的工作。)
The string \\s+
ist a regular expression which represents one or more space characters (space, newline, ...). (字符串\\s+
是一个正则表达式,代表一个或多个空格字符(空格,换行符,...)。)
You could also use " "
instead, but in this case your input must contain only one space character. (您也可以改用" "
,但是在这种情况下,您的输入必须仅包含一个空格字符。)
Example:
(例:)
System.out.println("Enter Name");
(System.out.println(“输入名称”);)
Input:
(输入:)
firstname secondname lastname
output:
(输出:)
firstname
secondname
lastname
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…