ÇÚ·Üѧϰjava£¬Óиö¹ØÓÚStringTokenizerµÄÎÊÌâimport java.util.*;
class MyTest
{
public static void main(String[] args)
{
String MyString="I love you. a b c s e f g ";
System.out.println(my1(MyString,7));
System.out.println(my1(MyString,2));
}
public static String my1(String s, int k)
{
StringTokenizer st=new StringTokenizer(s," \t\n\r,.;:");
String f=null;
for(int i=0;i<=st.countTokens();i++)
{
if(i==k)
f=st.nextToken();
st.nextToken();
}
return f;
}
}
£££££££££££££££££
ÓÐÕâô¸öÎÊÌâ
System.out.println(my1(MyString,7));µÄʱºòºóÃæÊä³öµÄÊÇnull
ÎÒ×Ô¼ºÈÏΪÊÇst.countTokens()³öÁËÎÊÌâ
»»³É st.hasMoreTokens()ÒÔºóµÃµ½½â¾ö
µ«ÊÇÎÒÔÚÀïÃæ¼ÓÁËSystem.out.println(st.countTokens());Êä³öÊÇÕýÈ·µÄ10¡£Ñ»·Ó¦¸Ã²»»á³öÎÊÌâ
²»ÖªµÀΪʲô»áÕâÑù>.<
Äĸöѧ³¤½Ì½ÌÎÒ
ллÁËÏÈ
[Ò¹²»¹éËÞ (4-28 11:59, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]1Â¥
»¹Óи½¼ÓÎÊÒ»¸öblueJµÄdebuggerÔõôÓÃ?[Ò¹²»¹éËÞ (4-28 12:04, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]2Â¥
С»ï×ÓÇÚ·Ü°¡£¡when u use StringTokenizer, in the for loop if u write for( int i =0; i< st.countTokens(); i++) then u may get wrong answer, coz st.countTokens() is variable. In the for loop, u will call st.nextToken(), then after that, st.countTokens() minus 1 already. So u may get null use your program. :st.countTokens() is always count the tokens left.
donot know what is blueJ.
r u lfj??? hehehe
[ÆåÖ÷ (4-28 14:25, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]3Â¥
>>>>>StringTokenizer st=new StringTokenizer(s," \t\n\r,.;:",fasle);
¼ÓÉÏfalse[³½ÐÇ (4-28 15:18, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]4Â¥
(ÒýÓà ҹ²»¹éËÞ:»¹Óи½¼ÓÎÊÒ»¸öblueJµÄdebuggerÔõôÓÃ?)>>>>>>ÓÃJ Creator²»»áËÀÈ˵ġ£[³½ÐÇ (4-28 15:18, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]5Â¥
(ÒýÓà ³½ÐÇ:>>>>>>ÓÃJ Creator²»»áËÀÈ˵ġ£)....ÎÒÏë¿´¿´³ÌÐòÔËÐеĹý³Ì,Ò»²½Ò»²½µÄdebug[Ò¹²»¹éËÞ (4-28 15:49, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]6Â¥
(ÒýÓà ³½ÐÇ:>>>>>StringTokenizer st=new StringTokenizer(s," \t\n\r,.;:",fasle); ¼ÓÉÏfalse)˵Çå³þµã,¼ÓfalseʲôÒâ˼>.<[Ò¹²»¹éËÞ (4-28 15:51, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]7Â¥
(ÒýÓà ÆåÖ÷:С»ï×ÓÇÚ·Ü°¡£¡when u use StringTokenizer, in the for loop if u write for( int i =0; i< st.countTokens(); i++) then u may get wro...)ллÁË:D˵µÄÕæÇå³þ.ps
ÎÒÊÇzzÓÎÏ·°æµÄ°ßÖñ[Ò¹²»¹éËÞ (4-28 15:53, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]8Â¥
Come in-->>//for(int i=0;i<=st.countTokens();i++)
here you can't use countTokens as loop condition, because it varies during execution. Once nextToken() executes once, the value of countTokens() decrements by one[cigar (4-28 16:45, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]9Â¥
(ÒýÓà cigar:Come in-->>//for(int i=0;i...)ллÁË£¬ÄǸöÔÀ´ÊDZäÁ¿>.<[Ò¹²»¹éËÞ (4-28 17:40, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]10Â¥
(ÒýÓà ҹ²»¹éËÞ:˵Çå³þµã,¼ÓfalseʲôÒâ˼>.)¼ÓfalseµÄÒâ˼ÊÇÀýÈçStringTokenizer(string,",. ",fasle);
ÄÇô¾Í²»»á·µ»Ø","ºÍ"."
ÄãÔÚÕâÀïÓÃStringTokenizer(s," \t\n\r,.;:");
ÄÇôÔÚÓöµ½"\t","\n",".",";"µÄʱºò£¬nextToken()Ò²»á·µ»Ø¡£
Èç¹û¼Ófalse£¬nextToken()ÔÚÓöµ½ÉÏÃæµÄÖ¸¶¨µÄstring£¬»áºöÂÔµôËüÃÇ¡£[³½ÐÇ (4-28 18:27, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]11Â¥
(ÒýÓà ҹ²»¹éËÞ:....ÎÒÏë¿´¿´³ÌÐòÔËÐеĹý³Ì,Ò»²½Ò»²½µÄdebug)>>>>>>>>×Ô¼ºÔÚ³ÌÐòÖмä¼Ó
System.out.println("Running Here");
System.out.println("i="+i);
System.out.println(.....);
[³½ÐÇ (4-28 18:28, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]12Â¥
(ÒýÓà ³½ÐÇ:¼ÓfalseµÄÒâ˼ÊÇÀýÈçStringTokenizer(string,",. ",fasle); ÄÇô¾Í²»»á·µ»Ø","ºÍ"." ÄãÔÚÕâÀïÓÃStringTokenizer(s," \t\n\r,.;:"); ÄÇ...)°¡¡£ºÃÏñĬÈϵľÍÊÇfalse¡£¡£²»ºÃÒâ˼:P[³½ÐÇ (4-28 18:29, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]13Â¥
¹ØÓÚdebugÂ¥ÏÂÒѾÓÐÈ˸æËßÄãÊÇʲôÔÒòÁË£¬ÎÒÀ´¸øÄã½²½âÒ»ÏÂÔõôdebugÕâ¸öprogram¡£
ͨ³£while loopÀïÖ´ÐеĶ«Î÷£¬²»ÊÇÎÒÃÇÏëÒªµÄ¶«Î÷£¬Ôõô°ì£¿°ÑÀïÃæÿ²½¶¼print³öÀ´£¬¿´¿´ÊÇÄIJ½³öÁËÎÊÌâ¡£
½«µÚ19ÐеÄst.nextToken();Àï¸Ä³É System.out.println(i+" "+st.nextToken());¡£È»ºóÔËÐгÌÐò£¬¿´¿´½á¹ûÈçºÎ¡£½á¹ûÈçÏ£º
0 I
1 love
2 you
3 a
4 b
5 c
null \\my1(MyString,7) µÄ½á¹û
0 I
1 love
2 a
3 b
4 c
you \\my1(MyString,2) µÄ½á¹û
ÄǸöwhile loopÔÚµÚÒ»´ÎÖ´ÐÐÁË5´Î¾ÍÍ£ÁË£¬ÔÚµÚ¶þ´Î£¬iµÄÖ¸ÊýÖ»Êǵ½4¾Í½áÊøÁË¡£ÎªÊ²Ã´£¿ÇëÄãÀ´·ÖÎö°É¡£:)[duck (4-29 16:07, Long long ago)]
[ ´«Í³°æ |
sForum ][µÇ¼ºó»Ø¸´]14Â¥
(ÒýÓà duck:¹ØÓÚdebugÂ¥ÏÂÒѾÓÐÈ˸æËßÄãÊÇʲôÔÒòÁË£¬ÎÒÀ´¸øÄã½²½âÒ»ÏÂÔõôdebugÕâ¸öprogram¡£ ͨ³£while loopÀïÖ´ÐеĶ«Î÷£¬²»ÊÇÎÒÃÇÏëÒªµÄ¶«Î÷£...)CS1102 µÄtutor :D ллduck[Ò¹²»¹éËÞ (4-29 23:41, Long long ago)] [ ´«Í³°æ | sForum ][µÇ¼ºó»Ø¸´]15Â¥