The Artima Developer Community
Sponsored Link

Java Answers Forum
How to solve this problem?

5 replies on 1 page. Most recent reply: Nov 7, 2003 12:33 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Will Shon

Posts: 1
Nickname: shyonggr
Registered: Nov, 2003

How to solve this problem? Posted: Nov 1, 2003 7:04 AM
Reply to this message Reply
Advertisement
Write a class that works as a filter for StringTokenizer. Your class should take a word length (>= 0) and a sentence (String) in its constructor and then it should return the next word from the sentence that is at least word length long on each subsequent call to its method nextWord().


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: How to solve this problem? Posted: Nov 1, 2003 6:27 PM
Reply to this message Reply
Lot of people are doing the same homework I guess :-)

Narayanan Premkumar

Posts: 2
Nickname: npk
Registered: Nov, 2003

Re: How to solve this problem? Posted: Nov 6, 2003 3:34 AM
Reply to this message Reply
Here's one way you could do this.

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
 
public class WordTokenizer implements Enumeration {
	
	private StringTokenizer tokenizer;
	int wordLength;
	private Iterator iterator;
	int tokens;
		
	public WordTokenizer(String str,int newWordLength){
		tokenizer = new StringTokenizer(str," ",false);
		wordLength = newWordLength;
		iterator = init();
	}
 
	public boolean hasMoreElements() {
		return iterator.hasNext();
	}
 
	public Object nextElement() {
		return iterator.next();
	}
 
	private Iterator init(){
		ArrayList list = new ArrayList();
		while(tokenizer.hasMoreTokens()){
			String token = tokenizer.nextToken();
			if( token.length()>=wordLength )
				list.add( token );
		}
		tokens = list.size();
		return list.iterator();
	}
 
	public boolean hasMoreWords() {
		return iterator.hasNext();
	}
 
	public String nextWord() {
		return (String)iterator.next();
	}
	
	public int countTokens(){
		return tokens;
	}
	
	public static void main(String[] args){
		WordTokenizer t = new WordTokenizer("Write a class that works as a filter for StringTokenizer",5);
		while( t.hasMoreWords() ){
			System.out.print( t.nextWord() + " " );			
		}
	}
}
 

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: How to solve this problem? Posted: Nov 6, 2003 4:20 AM
Reply to this message Reply
This problem has been already solved in the same forum

http://www.artima.com/forums/flat.jsp?forum=1&thread=19830

The reason why I also posted a solution in the above thread is because the poster has made an effort to write some code, unlike the original poster in this thread. Moreover before posting any question I guess they should search for a solution, if he had searched in this forum he/she would have found the solution.

I don’t think it’s a good idea to encourage people by just doing their home work.

Narayanan Premkumar

Posts: 2
Nickname: npk
Registered: Nov, 2003

Re: How to solve this problem? Posted: Nov 6, 2003 11:25 PM
Reply to this message Reply
Apologies.Didn't realise it had been solved already.Discovered later.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to solve this problem? Posted: Nov 7, 2003 12:33 AM
Reply to this message Reply
> Moreover before posting any question I guess they should
> search for a solution, if he had searched in this forum
> he/she would have found the solution.


Also, it would be courteous if the students would state what school, instructor and assignment number they are requesting the answer to. That way, it would save future students all the trouble of actually typing in the assignment description (which might involve actually thinking about it -- ugh! Can you imagine!?!).

Flat View: This topic has 5 replies on 1 page
Topic: Local server over Internet Previous Topic   Next Topic Topic: Lock Screen

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use