Results 1 to 5 of 5
- 11-23-2012, 08:01 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
Java Project that involves StringTokenizer Class and methods-need assistance
Ok, I know everyone needs background so here it is:
Project description:
Project… Military Censor
You are in the Army and have been assigned the task of censoring soldiers’ outgoing mail for
security reasons. Let’s assume that the troops all know about an upcoming offensive that will
involve an assault on the Hermes bridge that crosses the Muddy River. Develop an algorithm that
uses the StringTokenizer to examine each word of outgoing email. If any of the following words
are found, print the word REJECTED. If none are found, then print OK.
Taboo words are: Hermes, bridge, Muddy, River, assault, and offensive
Call your class Censor and use the following sentences for testing.
“I hope I survive the assault tomorrow.”
“I want to talk to you about Bobby, but we’ll cross that bridge later.”
“Tell sis and Larry that I’ll be Ok and I will see them in 6 months”
“Your last letter was a little muddy on exactly what you meant.”
“I see no point in us trying to take the hermes crossing.”
Notice the last sentence uses “hermes” instead of “Hermes”. Your code should not be sensitive to
case and should reject this sentence.
You should input these sentences via the keyboard using the Scanner class. Your output screen
should look like the following after testing all the sentences:
Enter next sentence: I hope I survive the assault tomorrow.
I hope I survive the assault tomorrow.>>>REJECTED
Enter next sentence: I want to ask about Bobby, but we'll cross that
bridge later.
I want to ask about Bobby, but we'll cross that bridge
later.>>>REJECTED
Enter next sentence: Tell sis and Larry that I'll be ok and I will see
them in 6 months.
Tell sis and Larry that I'll be ok and I will see them in 6
months.>>>OK
Enter next sentence: Your last letter was a little muddy on exactly
what you meant.
Your last letter was a little muddy on exactly what you
meant.>>>REJECTED
Enter next sentence: I see no point in us trying to take the hermes
crossing.
I see no point in us trying to take the hermes crossing.>>>REJECTED
So, I developed a program that was supposed to do that (Source code):
And here's what is printsJava Code:import java.io.*; import java.util.*; public class Brogram { public static void main(String args[]) { String s = " "; while (s.length() > 0) { int i = 0; System.out.print("Enter a new sentence: "); Scanner vv = new Scanner(System.in); s = vv.nextLine(); StringTokenizer t = new StringTokenizer(s, ", \n"); while (t.hasMoreTokens() == true) { i += i; if (t.nextToken().equalsIgnoreCase("bridge")) { i += 56; } else if (t.nextToken().equalsIgnoreCase("ASSAULT")) { i += 56; } else if (t.nextToken().equalsIgnoreCase("hermes")) { i += 56; } else if (t.nextToken().equalsIgnoreCase("offensive")) { i += 56; } else if (t.nextToken().equalsIgnoreCase("muddy")) { i += 56; } else { i += 0; } } if (i > 0) { System.out.println(s + "<<<REJECTED"); } else { System.out.println(s + "<<<ACCEPTED"); } } } }
Enter a new sentence: I'm crossing the hermes river
I'm crossing the hermes river<<<ACCEPTED
Enter a new sentence: Here hermes bridge muddy
On the second sentence, it gives me this error: java.util.NoSuchElementException: null (in java.util.StringTokenizer)
I would like for an experienced programmer to tell me why it runs like that, and how I can fix it.Thanks!Last edited by quad64bit; 11-23-2012 at 10:05 PM.
- 11-23-2012, 10:07 PM #2
Re: Java Project that involves StringTokenizer Class and methods-need assistance
You've hard coded in that it should read 5 words, but in your last example you only supply 4 words. So, the last word is null. You cannot compare a null to a String - hence the error.
P.S. I edited your post for readability.
- 11-23-2012, 10:14 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
Re: Java Project that involves StringTokenizer Class and methods-need assistance
So, how do i fix that? I've tried rearranging the if statements and it still gives me then null error. :/
- 11-24-2012, 01:24 AM #4
Re: Java Project that involves StringTokenizer Class and methods-need assistance
Well, part of the problem is that your code structure will never do what your project requirement describes. You are literally looking for certain works in an exact order- if I understand the assignment, you need to look for a collection of words in any order, not just first, second, third, fourth, and 5th word. I should be able to give you a 5000 word input and your app should be able to tell me if it contains bad words or not.
This implies that you need a loop for variable length input. Also, what if I gave you a list of 10,000 words that were bad words? Would you really write 10,000 if else statements? You're going about this in an awfully hard way.
- 11-24-2012, 03:47 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Need help with my project in Java class
By Adr in forum New To JavaReplies: 2Last Post: 09-06-2012, 12:47 PM -
JDBC class not found when calling a java class in a java project from my JSP in a web
By Mark1234 in forum New To JavaReplies: 6Last Post: 03-01-2012, 03:07 PM -
Java Collection class & methods
By box2box in forum New To JavaReplies: 2Last Post: 05-26-2010, 01:51 AM -
Creating Methods Assistance
By Desmond in forum New To JavaReplies: 10Last Post: 03-15-2010, 03:21 PM -
How can I call java class methods in Oracle 10g PL/SQL
By searcher34 in forum JDBCReplies: 0Last Post: 01-02-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks