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

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);
    }   
}

it doesn't exactly work so any help is appreciated.

Member Avatar for iamthwee

Hint: You are looking for substrings within a string.

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.

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

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);
    }   
}

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?

Member Avatar for iamthwee

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

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); 

   } 
}

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

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); 

   } 
}

That would work but he's expecting us to use StringTokenizer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.