Hey guys,

what would be a neat why of geting every third token in the string.

lets say there is a:

String longString= "i want this";

how would i be able to get "this" ?

Thanks

Recommended Answers

All 9 Replies

Use split to get he tokens in ann array
Loop thru the array processing every third element (int i = 2; i<array.length; i+=3)

@James, I think he means "words" by saying tokens, because, as he said, he wants "this" from "i want this". So I guess he wants to extract the third word.

[EDIT: Almost forgot this; dude gedas use a loop and a variable, name it as, say word_count and initialize it to 0. Loop through your string and increase word_count by 1 for every space you encounter. After the second space, you'll be on the third word. Loop through and record the String till the next space, and print to the screen.]

commented: James way is more efficient, thats what split() does. -1

@James, I think he means "words" by saying tokens, because, as he said, he wants "this" from "i want this". So I guess he wants to extract the third word.

Yes, that's what I assumed. That's why I suggested the approach that I did.

Both your ways work, even tho James is better than datin's. the problem is that the OP wants to use the StringTokenizer class for this.

String s = "one two three four five six seven eight nine ten eleven twelve thirteen";
		
    StringTokenizer st = new StringTokenizer(s);
		
    System.out.println("The string has " + st.countTokens() + " word" + (st.countTokens()>1?"s":"") + "!");
    st.nextToken();
    st.nextToken();
    System.out.println("The third word is " + st.nextToken());
		
    st = new StringTokenizer(s);
		
    System.out.println("\nEvery third words are :");
    String temp;
    int i = 0;
    while( st.hasMoreTokens() ){
        temp = st.nextToken();
        if((i+1)%3==0)System.out.println(temp);
        i++;
    }

thanks guys,

i like the way Philippe.Lahaie has overcome this issue, nonetheless i might have been a bit misleading stating that i want to use stringtokenizer, this is not essential and if the same result, however more efficiently could be achieved using different methods please share it with me.

thanks again for the advice given

String data =  "one two three four five six seven eight nine ten eleven twelve thirteen";

String[] words = data.split("\\s");
for (int i = 2; i<words.length; i+=3) System.out.println(words[i]);

I doubt that you will get much better than 2 simple lines of code...

Perfect !!

thanks to everyone for the help

indeed if your teacher didnt force StringTokenizer upon you then James' way is the best, hands down :)

Have I missed something? Where is the OP's statement about requiring StringTokenizer?

Anyway, gedas - if this is solved please mark it such and close the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.