为什么此 StringTokenizer 在第二次之后无法正确标记?

发布于 2024-08-11 13:51:38 字数 2019 浏览 2 评论 0原文

我想使用 StringTokenizer 为每个匹配 agent> 的字符串解析以下内容。我尝试使用这样的代码。我哪里错了?

StringTokenizer stringtokenizer=new StringTokenize(hari,"agent>");

while(stringtokenizer.hasMoreTokens())
{
    String token = stringtokenizer.nextToken();
    System.out.println("output ="+token);
}

它只正确标记两次,然后就响应不规则...... 我的意思是第一次

output =ACT-USER::USER_1:AU01::******;


   SIMULATOR 09-11-17 09:37:01
M  AU01 COMPLD
;

第二次:

output =RTRV-HDR:::RH01;


   SIMULATOR 09-11-17 09:37:01
M  RH01 COMPLD
;

第三次:

output =RTRV-EQPT::ALL:RE01;


   SIMULATOR 09-11-17 09:37:02
M  RE01 DENY
   SROF
   /*S
output =us, R
output =qu
output =s
output =d Op
output =r
output =io
output = F
output =il
output =d*/
;


ACT-USER::USER_1:AU01::******;


   SIMULATOR 09-11-17 09:37:01
M  AU01 COMPLD
;
agent>RTRV-HDR:::RH01;


   SIMULATOR 09-11-17 09:37:01
M  RH01 COMPLD
;
agent>RTRV-EQPT::ALL:RE01;


   SIMULATOR 09-11-17 09:37:02
M  RE01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA01;


   SIMULATOR 09-11-17 09:37:03
M  RA01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA02::MJ,;


   SIMULATOR 09-11-17 09:37:03
M  RA02 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA03::MN,;


   SIMULATOR 09-11-17 09:37:04
M  RA03 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA04::MN,T-LASERCURR-1-HIGH;


   SIMULATOR 09-11-17 09:37:04
M  RA04 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-PM-EQPT::ALL:RP01;


   SIMULATOR 09-11-17 09:37:05
M  RP01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>INIT-SYS::SLOT-1-1-2:IS01::1;


   SIMULATOR 09-11-17 09:37:05
M  IS01 COMPLD
;
agent>CANC-USER::USER_1:CU01;


   SIMULATOR 09-11-17 09:37:06
M  CU01 DENY
   IIAC
   /*Invalid Access Identifier*/
;
agent>

I want to parse the following using StringTokenizer for every string matching agent>. I tried it using the code like this. Where I am going wrong?

StringTokenizer stringtokenizer=new StringTokenize(hari,"agent>");

while(stringtokenizer.hasMoreTokens())
{
    String token = stringtokenizer.nextToken();
    System.out.println("output ="+token);
}

It is tokenizing properly only for two times and then it is responding irregularly...
I mean for first time

output =ACT-USER::USER_1:AU01::******;


   SIMULATOR 09-11-17 09:37:01
M  AU01 COMPLD
;

Second time:

output =RTRV-HDR:::RH01;


   SIMULATOR 09-11-17 09:37:01
M  RH01 COMPLD
;

Third time:

output =RTRV-EQPT::ALL:RE01;


   SIMULATOR 09-11-17 09:37:02
M  RE01 DENY
   SROF
   /*S
output =us, R
output =qu
output =s
output =d Op
output =r
output =io
output = F
output =il
output =d*/
;


ACT-USER::USER_1:AU01::******;


   SIMULATOR 09-11-17 09:37:01
M  AU01 COMPLD
;
agent>RTRV-HDR:::RH01;


   SIMULATOR 09-11-17 09:37:01
M  RH01 COMPLD
;
agent>RTRV-EQPT::ALL:RE01;


   SIMULATOR 09-11-17 09:37:02
M  RE01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA01;


   SIMULATOR 09-11-17 09:37:03
M  RA01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA02::MJ,;


   SIMULATOR 09-11-17 09:37:03
M  RA02 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA03::MN,;


   SIMULATOR 09-11-17 09:37:04
M  RA03 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-ALM-EQPT::ALL:RA04::MN,T-LASERCURR-1-HIGH;


   SIMULATOR 09-11-17 09:37:04
M  RA04 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>RTRV-PM-EQPT::ALL:RP01;


   SIMULATOR 09-11-17 09:37:05
M  RP01 DENY
   SROF
   /*Status, Requested Operation Failed*/
;
agent>INIT-SYS::SLOT-1-1-2:IS01::1;


   SIMULATOR 09-11-17 09:37:05
M  IS01 COMPLD
;
agent>CANC-USER::USER_1:CU01;


   SIMULATOR 09-11-17 09:37:06
M  CU01 DENY
   IIAC
   /*Invalid Access Identifier*/
;
agent>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2024-08-18 13:51:38

请告诉我我要去哪里
错了。

你没有读过API。 StringTokenizer 不接受字符串作为单个分隔符。它接受一个字符串,其中字符串中的每个字符都被视为一个字符分隔符。

尝试使用 String.split(...);

Please let me know where i am going
wrong.

You didn't read the API. StringTokenizer does not accept a string as a single delimiter. It accepts a string where each character in the string is treated as a one character delimiter.

Try using String.split(...);

春花秋月 2024-08-18 13:51:38

试试这个,

int idx=0; int tokenCount;
String words[]=new String [500];
String message=//String to be tokenize;
StringTokenizer st=new StringTokenizer(message);
tokenCount=st.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (st.hasMoreTokens()) // is there stuff to get?
    {words[idx]=st.nextToken(); idx++;}
for (idx=0;idx<tokenCount; idx++)
    { if(words[idx].equals("agent>");//do something

}

Try this,

int idx=0; int tokenCount;
String words[]=new String [500];
String message=//String to be tokenize;
StringTokenizer st=new StringTokenizer(message);
tokenCount=st.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (st.hasMoreTokens()) // is there stuff to get?
    {words[idx]=st.nextToken(); idx++;}
for (idx=0;idx<tokenCount; idx++)
    { if(words[idx].equals("agent>");//do something

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文