public class PersonName {
public static int NumberNames(String wholename)
{ // store the name passed in to the method
String testname = wholename;
// initialize number of names found
int numnames = 0;
// on each iteration remove one name
while (testname.length() > wholename.length())
{ // take the "white space" from the beginning and end
testname = testname.trim();
// determine the position of the first blank
// .. end of the first word
int posBlank = testname.indexOf(' ');
// cut off word
testname=testname.substring(posBlank + 1, testname.length());
System.out.println(numnames);
System.out.println(testname);
numnames++;
}
return numnames;
}
public static void main(String args[])
{
PersonName One= new PersonName();
System.out.println(One.NumberNames("Bobby"));
System.out.println(One.NumberNames("Bobby Smith"));
System.out.println(One.NumberNames("Bobby L. Smith"));
System.out.println(One.NumberNames(" Bobby Paul Smith Jr. "));
}
}
代码应该接受给定的字符串并输出一个结果,说明有多少字符串。到目前为止,我有这么多,但循环卡在“PersonName”方法的某个地方。有人知道为什么会被卡住吗?
答案 0 :(得分:1)
public static int NumberNames(String t){
if(t == null || t.length() == 0) return 0;
return t.trim().split("\\s+").length;
}
这不容易吗?
编辑:
如果你想使用你的实际代码(只是修改了一下):
public class PersonName
{
public static int NumberNames(String wholename)
{
// store the name passed in to the method
// and replace multiple whitespaces for a single space
// and take the "white space" from the beginning and end
String testname = wholename.replaceAll("\\s+", " ").trim();
int testnameLength = testname.length();
// initialize number of names found
int numnames = 0;
// on each iteration remove one name
while (testnameLength > 0)
{
// determine the position of the first blank
// .. end of the first word
int posBlank = testname.indexOf(' ');
// if we reached the last word, break the while-loop
if(posBlank == -1)
{
numnames++;
break;
}
// cut off word
testname = testname.substring(posBlank + 1, testnameLength);
testnameLength = testname.length();
//System.out.println(numnames);
//System.out.println(testname);
numnames++;
}
return numnames;
}
public static void main(String args[])
{
PersonName One= new PersonName();
System.out.println(One.NumberNames("Bobby"));
System.out.println(One.NumberNames("Bobby Smith"));
System.out.println(One.NumberNames("Bobby L. Smith"));
System.out.println(One.NumberNames(" Bobby Paul Smith Jr. "));
}
}
我也提出了两个想法:
答案 1 :(得分:0)
你的问题在于你的循环条件:
while (testname.length() > 0) // this will always be true and is causing your infinite loop
我不确定你要在这里完成什么,但这就是为什么你的程序被“卡住”了。
答案 2 :(得分:0)
while(testname.length()> wholename.length())
只要testname中的字符数高于wholename中的字符数,您的while循环就会为true。这是永远不会的,因为你在循环之前使它们彼此相等。你应该改变一些东西,但我不知道是什么,因为我不确定你想要什么样的结果:)
答案 3 :(得分:0)
while循环应该是这样的:
while (testname.length()>numnames)
答案 4 :(得分:0)
如果更改为循环条件testname.length() > 0
,如果名称末尾没有空格,循环将永远不会终止。
当字符串为"Bobby"
时,请考虑第一次调用中的情况:
int posBlank = testname.indexOf(' ');
没有空格,因此posBlank
为-1
。
// cut off word
testname = testname.substring(posBlank + 1, testname.length());
子字符串调用的参数是-1 + 1
,0
和5
。
旧字符串为"Bobby"
,新字符串为"Bobby"
,循环将永不终止。