| | |
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? <?>
Views: 1855 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
add android applet application arguments array arraylist arrays c# c++ chat class classes client code component constructor convert coordinates data database db desktop detection eclipse error event exception file forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image images inheritance input integer interface j2me java javafx jframe jpanel jtextarea jtextfield key lazy linked linked-list list loop looping method methods mobile netbeans newbie node number object oracle output page parameter pattern pixel problem programming read recursion remove return robot scanner search server service set size sms software sql string swing system text text-file threads time timer translate tree user






