943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2214
  • Java RSS
Jan 31st, 2007
0

Help with Censor.

Expand 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
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Talon is offline Offline
14 posts
since Oct 2006
Jan 31st, 2007
0

Re: Help with Censor.

Hint: You are looking for substrings within a string.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jan 31st, 2007
0

Re: Help with Censor.

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Talon is offline Offline
14 posts
since Oct 2006
Jan 31st, 2007
0

Re: Help with Censor.

Click to Expand / Collapse  Quote originally posted by Talon ...
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
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Talon is offline Offline
14 posts
since Oct 2006
Feb 1st, 2007
0

Re: Help with Censor.

Why don't u use nice standard functions...

Java Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 1st, 2007
0

Re: Help with Censor.

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Why don't u use nice standard functions...

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Talon is offline Offline
14 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help!!!
Next Thread in Java Forum Timeline: Super-power compare method? <?>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC