Results 1 to 5 of 5
  1. #1
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default 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):
    Java 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");
                }
            }
        }
    }
    And here's what is prints
    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.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.

  3. #3
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default 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. :/

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.

  5. #5
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default Re: Java Project that involves StringTokenizer Class and methods-need assistance

    Ok, so I merged the statements into 1 single if-statement with the boolean operator Or, but it still doesn't compile. Am I over-analyzing or is the code being funky?

Similar Threads

  1. Need help with my project in Java class
    By Adr in forum New To Java
    Replies: 2
    Last Post: 09-06-2012, 12:47 PM
  2. Replies: 6
    Last Post: 03-01-2012, 03:07 PM
  3. Java Collection class & methods
    By box2box in forum New To Java
    Replies: 2
    Last Post: 05-26-2010, 01:51 AM
  4. Creating Methods Assistance
    By Desmond in forum New To Java
    Replies: 10
    Last Post: 03-15-2010, 03:21 PM
  5. Replies: 0
    Last Post: 01-02-2008, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •