User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 422,940 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,571 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 43102 | Replies: 9
Reply
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Java's String Tokenizer

  #1  
Jul 13th, 2003
Hi. I have just been introduced to string tokenizer in one of the java lessons. It can remove characters from an array of strings. So, I am wondering if it could remove a string from an array of string. For example: "the fish was eaten by the cat". I'd like to remove "the" from this array of string, resulting to: "fish was eaten by cat", without getting all t's, h's and e's from the array of string. Is this possible? :roll:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2003
Posts: 11
Reputation: sooim3 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
sooim3 sooim3 is offline Offline
Newbie Poster

Re: Java's String Tokenizer

  #2  
Jul 14th, 2003
AFAIK, the StringTokenizer does not in itself remove characters; it breaks up a string in tokens at given delimeters. If StringTokenizer is constructed like this:

StringTokenizer st=new StringTokenizer("the fish was eaten by the cat");

...the default delimeters are:
- space character
- the tab character
- newline character
- carriage-return character
- form-feed character

If you want to remove the 'the' from your string, then the above construction would work, since the sentence consists of space characters that delimits each word.

And one way to remove the 'the' from the string, is using a simple little loop to check if the word, a token, is equals to 'the' or not.

Like such:

Create a new output string
Construct the StringTokenizer with the string that is to be parsed ("the fish was eaten by the cat")

Check each word with a loop:
While there is more tokens
Create a new temporary string with the nextToken()
If the temporary string does not equals 'the'
Add temporary string to the output string
(else do nothing)
print out the outputstring

...That should result in: "fish was eaten by cat"

Hope this is of any help,
/Soo-Im
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Java's String Tokenizer

  #3  
Jul 14th, 2003
The String tokenizer doesn't have a method to remove tokens. If you have a string like "the fish was eaten by the cat" rather than going through the overhead of creating more objects, just use the native method "replace" or "replaceall" that comes with the String object. You can replace the "the" with an empty string and thus take it out.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Java's String Tokenizer

  #4  
Jul 14th, 2003
Thank you so much...So, here I am, trying with those suggestions given...

class extra{
     public static void main(String[] args){
          String output = "";
          for(int i=0; i<args.length; i++){
                if(args[i] != "the")
                       output+= args[i];
          }
          System.out.println("Results: "+output);
          System.exit(0);
     }
}

I thought the above might work, without using StringTokenizer....but, hmph...it turned out to print the whole thing!
I have tried using sooim3's suggestion, but it didnt work as well, maybe my code is wrong...
I'm not sure...
Reply With Quote  
Join Date: Jun 2003
Posts: 11
Reputation: sooim3 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
sooim3 sooim3 is offline Offline
Newbie Poster

Re: Java's String Tokenizer

  #5  
Jul 14th, 2003
Uhm, my suggestion wasn't at all as good as the one that followed... you could try this:

public class TokenTest {

public static void main(String[] args) {
System.out.println("the fish was eaten by the cat".replaceAll("the", ""));
}
}

/Soo-Im
Reply With Quote  
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Java's String Tokenizer

  #6  
Jul 14th, 2003
Cool~
Thanks...hehe...guess I've learned something new here. Btw, this means that replaceAll is a method in java.lang.String?
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Java's String Tokenizer

  #7  
Jul 14th, 2003
Yes. Check out the Java API docs for String:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

The whole J2SE API:
http://java.sun.com/j2se/1.4.2/docs/api/
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jul 2003
Posts: 4
Reputation: lohengrin332 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lohengrin332 lohengrin332 is offline Offline
Newbie Poster

Re: Java's String Tokenizer

  #8  
Jul 16th, 2003
This code will work if you use
!args[i].equals("the")
instead of
args[i] != "the"
. The latter doesn't work because you end up comparing the location in memory rather than the contents of the memory, like when you compare pointers in C without dereferencing them. Sometimes it will work, but usually it will not.

Originally Posted by red_evolve

class extra{
     public static void main(String[] args){
          String output = "";
          for(int i=0; i<args.length; i++){
                if(args[i] != "the")
                       output+= args[i];
          }
          System.out.println("Results: "+output);
          System.exit(0);
     }
}
Reply With Quote  
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Java's String Tokenizer

  #9  
Jul 16th, 2003
wow! thanks so much!
Reply With Quote  
Join Date: Mar 2005
Posts: 5
Reputation: mackverick is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mackverick mackverick is offline Offline
Newbie Poster

Re: Java's String Tokenizer

  #10  
Mar 17th, 2005
Well i wasn't sure where to post my question but since you guys are talking about tokens and all that, i have a question...

this is my first Java course and i am having troubles doing one of the assignments... well i am suppose to read a file containing grades of students...here is an example

034-23-8901 45 78 85 34
342-67-1231 98 45 67 45
452-57-2351 49 78 61 52

the first 9 digits are the social security of the student and the other numbers are the grades. well i am suppose to open this file ( it is named Grades.txt) somehow i am suppose to read the grades and find the average if the student has an average of 90 or above he will be excluded from the final and then my program has to write an outFile named Exemption... it seems very easy but i just can't get it to work...

My question is do i have to use the tokenizer to separate the numbers... if it is possible how can i do it?

Please i will appreciate any help...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC