| | |
Help with Censor.
![]() |
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
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 it doesn't exactly work so any help is appreciated.
Java Syntax (Toggle Plain Text)
import TerminalIO.*; import java.util.*; public class Censor { public static void main(String args[]) { KeyboardReader reader = new KeyboardReader(); System.out.println("Enter next sentence: "); String s = reader.readLine(); StringTokenizer stk = new StringTokenizer(s); String ac = ">>>ACCEPTED"; for(int j = 0; j < stk.countTokens();j++) { String nxt = s.nextToken(); if(nxt.equalsIgnoreCase("hermes")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("bridge")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("Muddy")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("River")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("assault")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("offensive")) ac = ">>>REJECTED"; } System.out.println(s + ac); } }
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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 farit doesn't exactly work so any help is appreciated.Java Syntax (Toggle Plain Text)
import TerminalIO.*; import java.util.*; public class Censor { public static void main(String args[]) { KeyboardReader reader = new KeyboardReader(); System.out.println("Enter next sentence: "); String s = reader.readLine(); StringTokenizer stk = new StringTokenizer(s); String ac = ">>>ACCEPTED"; for(int j = 0; j < stk.countTokens();j++) { String nxt = stk.nextToken(); if(nxt.equalsIgnoreCase("hermes")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("bridge")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("Muddy")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("River")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("assault")) ac = ">>>REJECTED"; else if(nxt.equalsIgnoreCase("offensive")) ac = ">>>REJECTED"; } System.out.println(s + ac); } }
Why don't u use nice standard functions...
Java Syntax (Toggle Plain Text)
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { String sentence = "hello there";//change it here String ac = ">>>ACCEPTED"; String[] temp = null; temp = sentence.split(" "); // split sentence into tokens using the space as // a divider for (int j = 0; j < temp.length; j++ ) { if (temp[j].equals("bridge")) { ac = ">>>>rejected"; } else if (temp[j].equals("River")) { ac = ">>>>rejected"; } } System.out.println(sentence + " " + ac); } }
*Voted best profile in the world*
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
Why don't u use nice standard functions...
Java Syntax (Toggle Plain Text)
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { String sentence = "hello there";//change it here String ac = ">>>ACCEPTED"; String[] temp = null; temp = sentence.split(" "); // split sentence into tokens using the space as // a divider for (int j = 0; j < temp.length; j++ ) { if (temp[j].equals("bridge")) { ac = ">>>>rejected"; } else if (temp[j].equals("River")) { ac = ">>>>rejected"; } } System.out.println(sentence + " " + ac); } }
![]() |
Similar Threads
- writing a censor program from a file help (Python)
- Potential Unwanted Behaviour (Viruses, Spyware and other Nasties)
- My Progress with DollarBuddy (Advertising Sales Strategies)
- DaniWeb Poll: Should member locations be displayed in the threads? (DaniWeb Community Feedback)
- GoDaddy Commercial (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: help!!!
- Next Thread: Super-power compare method? <?>
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






