| Author |
String Tokenizer - Unable to get the Proper Output
|
shivamahesh bachu
Greenhorn
Joined: Sep 14, 2012
Posts: 13
|
|
Hi Guys,
i am getting wrong output
Expected output:-
f:\a\c\
actual output :-
a\c\
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1409
|
|
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?
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
Why are you using a tokenizer at all? Have you read its documentation?
|
 |
vikky agrawal
Ranch Hand
Joined: Dec 18, 2008
Posts: 64
|
|
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
|
scjp 6
http://algorithmsea.blogspot.com
|
 |
shivamahesh bachu
Greenhorn
Joined: Sep 14, 2012
Posts: 13
|
|
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
Joined: Sep 14, 2012
Posts: 13
|
|
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
Joined: Dec 18, 2008
Posts: 64
|
|
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
Joined: Mar 17, 2011
Posts: 4761
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
shivamahesh bachu
Greenhorn
Joined: Sep 14, 2012
Posts: 13
|
|
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
Joined: Oct 13, 2005
Posts: 32708
|
|
shivamahesh bachu wrote: . . . but he should be specific like you did.
No, we expect you to be able to read the documentation.
|
 |
 |
|
|
subject: String Tokenizer - Unable to get the Proper Output
|
|
|