Help with Censor.

Reply

Join Date: Oct 2006
Posts: 14
Reputation: Talon is an unknown quantity at this point 
Solved Threads: 0
Talon Talon is offline Offline
Newbie Poster

Help with Censor.

 
0
  #1
Jan 31st, 2007
Hi, I've got a project in which I need the program to reject a sentence if it has a certain word in it using StringTokenizer. Here is what I have so far
  1. import TerminalIO.*;
  2. import java.util.*;
  3. public class Censor
  4. {
  5. public static void main(String args[])
  6. {
  7. KeyboardReader reader = new KeyboardReader();
  8. System.out.println("Enter next sentence: ");
  9. String s = reader.readLine();
  10. StringTokenizer stk = new StringTokenizer(s);
  11. String ac = ">>>ACCEPTED";
  12. for(int j = 0; j < stk.countTokens();j++)
  13. {
  14. String nxt = s.nextToken();
  15.  
  16. if(nxt.equalsIgnoreCase("hermes"))
  17. ac = ">>>REJECTED";
  18. else if(nxt.equalsIgnoreCase("bridge"))
  19. ac = ">>>REJECTED";
  20. else if(nxt.equalsIgnoreCase("Muddy"))
  21. ac = ">>>REJECTED";
  22. else if(nxt.equalsIgnoreCase("River"))
  23. ac = ">>>REJECTED";
  24. else if(nxt.equalsIgnoreCase("assault"))
  25. ac = ">>>REJECTED";
  26. else if(nxt.equalsIgnoreCase("offensive"))
  27. ac = ">>>REJECTED";
  28.  
  29. }
  30. System.out.println(s + ac);
  31. }
  32. }
it doesn't exactly work so any help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with Censor.

 
0
  #2
Jan 31st, 2007
Hint: You are looking for substrings within a string.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: Talon is an unknown quantity at this point 
Solved Threads: 0
Talon Talon is offline Offline
Newbie Poster

Re: Help with Censor.

 
0
  #3
Jan 31st, 2007
Originally Posted by iamthwee View Post
Hint: You are looking for substrings within a string.
I haven't once seen anyone use a substring for this lesson. They've used do-while loops but I can't seem to figure one out. I can't really get a hold of this StringTokenizer class. It's a bit confusing. I understand the concepts but putting it to use is much harder. I think that it doesn't work because of the "Enter next sentence:" has only 4 tokens that are counted when stk.countTokens() is called because it'll work if it's at spot 0,1,2,or 3 but after that...nothing.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: Talon is an unknown quantity at this point 
Solved Threads: 0
Talon Talon is offline Offline
Newbie Poster

Re: Help with Censor.

 
0
  #4
Jan 31st, 2007
Originally Posted by Talon View Post
Hi, I've got a project in which I need the program to reject a sentence if it has a certain word in it using StringTokenizer. Here is what I have so far
  1. import TerminalIO.*;
  2. import java.util.*;
  3. public class Censor
  4. {
  5. public static void main(String args[])
  6. {
  7. KeyboardReader reader = new KeyboardReader();
  8. System.out.println("Enter next sentence: ");
  9. String s = reader.readLine();
  10. StringTokenizer stk = new StringTokenizer(s);
  11. String ac = ">>>ACCEPTED";
  12. for(int j = 0; j < stk.countTokens();j++)
  13. {
  14. String nxt = stk.nextToken();
  15.  
  16. if(nxt.equalsIgnoreCase("hermes"))
  17. ac = ">>>REJECTED";
  18. else if(nxt.equalsIgnoreCase("bridge"))
  19. ac = ">>>REJECTED";
  20. else if(nxt.equalsIgnoreCase("Muddy"))
  21. ac = ">>>REJECTED";
  22. else if(nxt.equalsIgnoreCase("River"))
  23. ac = ">>>REJECTED";
  24. else if(nxt.equalsIgnoreCase("assault"))
  25. ac = ">>>REJECTED";
  26. else if(nxt.equalsIgnoreCase("offensive"))
  27. ac = ">>>REJECTED";
  28.  
  29. }
  30. System.out.println(s + ac);
  31. }
  32. }
it doesn't exactly work so any help is appreciated.
I had to change the s.nextToken() to stk.nextToken() that's what the code is. Now, any advice?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with Censor.

 
0
  #5
Feb 1st, 2007
Why don't u use nice standard functions...

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. String sentence = "hello there";//change it here
  9.  
  10. String ac = ">>>ACCEPTED";
  11.  
  12. String[] temp = null;
  13. temp = sentence.split(" ");
  14. // split sentence into tokens using the space as
  15. // a divider
  16.  
  17. for (int j = 0; j < temp.length; j++ )
  18. {
  19. if (temp[j].equals("bridge"))
  20. {
  21. ac = ">>>>rejected";
  22. }
  23.  
  24. else if (temp[j].equals("River"))
  25. {
  26. ac = ">>>>rejected";
  27. }
  28. }
  29.  
  30. System.out.println(sentence + " " + ac);
  31.  
  32. }
  33. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: Talon is an unknown quantity at this point 
Solved Threads: 0
Talon Talon is offline Offline
Newbie Poster

Re: Help with Censor.

 
0
  #6
Feb 1st, 2007
Originally Posted by iamthwee View Post
Why don't u use nice standard functions...

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6. public static void main(String args[])
  7. {
  8. String sentence = "hello there";//change it here
  9.  
  10. String ac = ">>>ACCEPTED";
  11.  
  12. String[] temp = null;
  13. temp = sentence.split(" ");
  14. // split sentence into tokens using the space as
  15. // a divider
  16.  
  17. for (int j = 0; j < temp.length; j++ )
  18. {
  19. if (temp[j].equals("bridge"))
  20. {
  21. ac = ">>>>rejected";
  22. }
  23.  
  24. else if (temp[j].equals("River"))
  25. {
  26. ac = ">>>>rejected";
  27. }
  28. }
  29.  
  30. System.out.println(sentence + " " + ac);
  31.  
  32. }
  33. }
That would work but he's expecting us to use StringTokenizer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1855 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC