The Artima Developer Community
Sponsored Link

Java Answers Forum
a menthod to print a token by token each time you call

5 replies on 1 page. Most recent reply: Nov 1, 2003 9:12 PM by jung

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
jung

Posts: 5
Nickname: jung
Registered: Oct, 2003

a menthod to print a token by token each time you call Posted: Oct 31, 2003 10:41 PM
Reply to this message Reply
Advertisement
When i am using while loop to break a sentence into token
it will print out all token in one time.
Now i need to creat a method to print out just one token
which length >= my interger number, if i call that method again, it will print out the second token which satisfy the condition and so on until the end of sentence.
Any help will be greatful!!


Vu Che

Posts: 8
Nickname: bluegreen
Registered: Oct, 2003

Re: a menthod to print a token by token each time you call Posted: Nov 1, 2003 3:23 AM
Reply to this message Reply
I offer you 2 methods for your reference:

One:
you pop out the first token as you ever did, then you create another string containing only the rest of tokens. Then continue from the beginning.

Two:
you use a static int variable to keep the position of the token that you need to pop out. You increase the value of it by 1 after a pop action. The next time you just need to make a ref to the int variable to know which token u need to pop out.

hope it helps u

jung

Posts: 5
Nickname: jung
Registered: Oct, 2003

Re: a menthod to print a token by token each time you call Posted: Nov 1, 2003 8:18 AM
Reply to this message Reply
Thanks, bluegreen, but i'm too stupid to transfer your words to codes, so i just post my questions and codes, hope you or anyone can help me to work this out. Thanks!

This is my assignment:
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().
When you implement your class you can use StringTokenizer to tokenize the sentence into words
And this is my code so far:
public class WordFilter{
  String temp;
  String sentence1;
  int	wordLength1;
 	 	
 public WordFilter(String sentence, int wordLength){
 	 	
 	 sentence1 = sentence;
 	 wordLength1 = wordLength;
  		
 	}
 
 public String NextWord(){
 	
 	StringTokenizer st= new StringTokenizer(sentence1);
 		int number = st.countTokens();
		String strArr[] = new String[number];
		for( int i=0; st.hasMoreTokens(); i++){
			if(strArr.length >= wordLength1)
				strArr[i] = st.nextToken();				temp = strArr[i];
		}return temp;
  
 	}
}
 
public class WordFilterTest{
	
public static void main (String[] args) throws Exception {
WordFilter x = new WordFilter("you are really great!",4);
x.NextWord();
System.out.println(x.NextWord());
 


the problem i have are two:
1.this code do not check token length
2.it can not printout a token properly.

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: a menthod to print a token by token each time you call Posted: Nov 1, 2003 11:31 AM
Reply to this message Reply
I would try something like this
public class WordFilter{
  String temp;
  String[] sentence1;
  int	wordLength1;
  int	index;
public WordFilter(String sentence, int wordLength){
	sentence1 = sentence.split("\\s");
 	wordLength1 = wordLength;
	index = 0;
   }
public String NextWord(){
	boolean continue = true;
	while(index < sentence1.length && continue){
		temp = sentence1[index];
		if(temp.length >= wordLength1)
			continue = false;
		index++;
	}
	if (continue)
		return "";
	else
		return temp;
   }
}

Break the sentence into an array of string when you get it and set the index to 0, then on nextWord() use that index to see if the words following that are greater the the word length sent into the contructor. If it is "continue" is false, send back temp, otherwise no String was found with a greater lengthso send back an empty string because you must be at the end of the array.

I am not sure what you want to do when the end of the array arrives so i just made it send back the empty String. I just typed that all in but something like that should work.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a menthod to print a token by token each time you call Posted: Nov 1, 2003 5:55 PM
Reply to this message Reply
Hello Jonathon,

Don't post code to forums which won’t even compile. The fact that you have used continue in your code as a variable name shows how careless you were when you were writing the code and it has not been tested at all. It’s good to help people but at the same time I think we should direct them in the right path. We shouldn't help them for sake of helping.

I have modified the code a little so that a person calling the nextWord will know whether actually a next word exist before calling saving some loop iterations.

/**
 * <p>Counting words: </p>
 * <p>Company: Someones homework</p>
 * @author P Senthoorkumaran
 * @version 1.0
 */
import java.util.*;
 
public class StringToken {
 
  List tokens = new ArrayList();
  int index;
 
  public StringToken(String sentence, int wordLength) {
    String[] buffer = sentence.split(" ");;
    int count = 0;
    for (int i = 0; i < buffer.length; i++)
      if (buffer[i].length() >= wordLength)
       tokens.add(buffer[i]);
    index = 0;
  }
 
 public boolean isTokenAvailable(){
   return  index < tokens.size();
 }
  public String nextWord() {
    return (String)tokens.get(index++);
  }
 
  public static void main(String args[]){
    StringToken myToken = new StringToken("Don't ever post code without testing them", 5);
    while (myToken.isTokenAvailable())
      System.out.println("Token is " + myToken.nextWord());
  }
}

jung

Posts: 5
Nickname: jung
Registered: Oct, 2003

Re: a menthod to print a token by token each time you call Posted: Nov 1, 2003 9:12 PM
Reply to this message Reply
Thanks to jonathon anyway.
and very grateful goes to Senthoor, i learnd a lot from
your code. appreciate!

Flat View: This topic has 5 replies on 1 page
Topic: java pipes Previous Topic   Next Topic Topic: tomcat and hssf

Sponsored Links



Google
  Web Artima.com   

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