java countTokens()方法

来源:百度知道 编辑:UC知道 时间:2024/05/05 20:41:12
import java.util.*;

public class h {

public void sl(String str){
StringTokenizer st = new StringTokenizer(str);
int c = st.countTokens();
System.out.println(c);
}

public static void main(String[] args) {
// TODO Auto-generated method stub

h jj = new h();
Scanner sc = new Scanner(System.in);
System.out.print("input a string");
String a = sc.next();
jj.sl(a);

}

}
为什么我输入hello world,最后显示结果为1.

import java.util.*;

public class h {

public void sl(String str){
StringTokenizer st = new StringTokenizer(str);
int c = st.countTokens();
System.out.println(c);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
h jj = new h();
Scanner sc = new Scanner(System.in);
System.out.print("input a string");
String a;
while (sc.hasNext()) {
a = sc.next();
jj.sl(a);
}
sc.close();
}
}