This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer - Unable to get the Proper Output

 
shivamahesh bachu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

i am getting wrong output


Expected output:-

f:\a\c\

actual output :-

a\c\
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense when you realize what happens in the if-else construct.
In the first iteration, what do you think the nextElement() call in the if-condition returns, and how does it affect the current position of the StringTokenizer? Likewise, what would the effect of the nextToken() call in the else-body be?

 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a tokenizer at all? Have you read its documentation?
 
vikky agrawal
Ranch Hand
Posts: 65
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now when you run this program,
if (st.nextElement().equals("d"))

if block will advance the tokenizer to next token i.e from f->a then again from b->c that is the reason you are getting output as a\c\, never think compiler is wrong.!
and according to you if you are not concentrating on if block then expected output should be f:\a\b\c
 
shivamahesh bachu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using a tokenizer at all? Have you read its documentation?



Why do we use a tokenizer ?

check out java docs ....
 
shivamahesh bachu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikky agrawal wrote:

Now when you run this program,
if (st.nextElement().equals("d"))

if block will advance the tokenizer to next token i.e from f->a then again from b->c that is the reason you are getting output as a\c\, never think compiler is wrong.!
and according to you if you are not concentrating on if block then expected output should be f:\a\b\c




Mr. vikky agrawal,

Thanks for your explanation
anyways i have resolved it long back.

I did this:-

import java.util.StringTokenizer;

public class Tok {

public static void main(String args[]) {

String source = "f:\\a\\b\\c\\d\\e";
StringTokenizer st = new StringTokenizer(source, "\\");

String da = "";
try{
while (st.hasMoreTokens()) {

String ss = st.nextToken();

// System.out.println("Tokens "+st.nextToken());

if (ss.equals("d"))
{
//System.out.println("Entered with d");
break;
}
else{
da = da + ss + "\\";
}


}
}
catch(Exception e)
{
e.getMessage();
}


System.out.println(da);


}
}


My Output Was :-


f:\a\b\c\
 
vikky agrawal
Ranch Hand
Posts: 65
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. vikky agrawal,

Thanks for your explanation
anyways i have resolved it long back


That is good, but I gave the explanation for the understanding of the reason that why output was not according to your expectation, hope this helps.
Enjoy
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivamahesh bachu wrote:Why do we use a tokenizer ?
check out java docs ....

He has. Specifically the part that says: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code."

That's why Campbell asked the question. And if he hadn't, I would have.

Winston
 
shivamahesh bachu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
shivamahesh bachu wrote:Why do we use a tokenizer ?
check out java docs ....

He has. Specifically the part that says: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code."

That's why Campbell asked the question. And if he hadn't, I would have.

Winston





Mr. Winston ,

but he should be specific like you did.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivamahesh bachu wrote: . . . but he should be specific like you did.

No, we expect you to be able to read the documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic