• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

String Tokenizer - Unable to get the Proper Output

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

i am getting wrong output


Expected output:-

f:\a\c\

actual output :-

a\c\
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using a tokenizer at all? Have you read its documentation?
 
Ranch Hand
Posts: 65
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76395
364
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic