#16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Are you looking at the character before "cat" and the character after "cat"?
    If you look at the character after "cat" in "category" it is an "e".
    "e" is not a valid delimiter so "category" is not a match.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    I've shown you my code, that is all I've done. I have messed around with spaces before and after cat, but they don't completely solve the problem for me. " cat" or "cat " does not solve the problem.
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Have you seen any of my posts about looking at the characters before and after where the searched for String is found? I called them the delimiters of the String. The character that is immediately before and the one immediately after the found String.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    Originally Posted by NormR
    You need to define all the ways a word can be put in a String. For example:
    Starts the String and is followed with a delimiter like space, comma(,), period(.), end of String, etc
    Follows a delimiter like a space, comma, etc and is followed by a delimiter or by the end of the String.
    Punctuation is not necessary for this assignment, so the period and comma do not need to be dealt with. Aside from that, you've suggested spacing variations, which I have told you multiple times do not solve the problem. You don't seem to understand this.
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    I'm suggesting that you look at the character after the "t" after you have found "cat"

    You don't seem to understand this.
    I think I do. I've written a few dozen word scanners like this.

    I guess what I don't understand is why you don't see what I'm describing as a solution: test the delimiters for "cat"
    What char is before "cat" and what char is after "cat"
    Last edited by NormR; February 10th, 2013 at 05:52 PM.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    I have messed around with space delimiters and they don't work. I don't doubt your ability, but I've been programming for three weeks, and your cryptic suggestions are not effective. I'm not asking for you to do my assignment for me, but I am stumped at figuring out how to figure this out. Is it an issue of using a method other than indexOf?
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Can you get the char before where "cat" is found and look at it?

    Can you get the char after where "cat" is found and look at it?


    using a method other than indexOf
    Yes this requires using other String class methods like length, charAt and substring. Look at the API doc for the String class.
    Last edited by NormR; February 10th, 2013 at 07:43 PM.
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    Code:
    public static void main(String[] args) 
        {
             String s1, s2;
        
        System.out.println("Enter a sentence:");
        Scanner keyboard = new Scanner(System.in);
        s1 = keyboard.nextLine();
        
        s2 = s1.toLowerCase();
        boolean n1 = s2.contentEquals("cat");
        boolean n2 = s2.contentEquals( " cat");
        boolean n3 = s2.contentEquals( "cat ");
        
        
        
        if (n1 = true)
       {
           System.out.println("Contains Profanity.");
       }
      else
           if (n2 = true)
           {
               System.out.println("Contains Profanity.");
           }
       else
               if (n3 = true)
               {
                   System.out.println("Contains Profanity.");
               } 
       else
                   if ((n1 = false) && (n2=false) && (n3=false))
                           {        
                   System.out.println(s1);
                           }
    Why does this censor everything as profanity? When I test n1, n2, and n3 by themselves, they return false values when I type "hello there," and true for cat and its spacing variants. So how come my if statements get triggered when I type "hello there"?

    I know you said to us length, charAt, and substrings, but this contentEquals method seems like it could work.
  16. #24
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    The = operator is for assignment. An assignment statement returns the value of the left hand operand.
    The == operator is for testing equality.
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    D'oh! That was a really dumb mistake, I should know better than that. >_<

    With individual words, the contentEquals works great...but when I started writing complete sentences, it does not work. I'm working on doing it the way you told me too.
  20. #26
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Look at the methods that will return a character from a String at a certain position in the String. charAt() and substring() will do it.
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    Code:
    class JavaApplication25
    {
    
       public static void main(String[] args) 
       {
                String s1;
        
        System.out.println("Enter a sentence:");
        Scanner keyboard = new Scanner(System.in);
        s1 = keyboard.nextLine();
        
        String s2 = s1.toLowerCase();
        int index1 = s2.indexOf("cat");
        int index4 = s2.indexOf(" cat");
        int index5 = s2.indexOf("cat ");
        int index2 = s2.indexOf("dog");
        int index3 = s2.indexOf("llama");
        
        
        
        if ((index1 < 0) && (index2 <0) && (index3 <0))
        {
            System.out.println(s1);
        }
        else 
            if ((index1 >= -1) || (index2>=-1) || (index3 >= -1) || (index4 >= -1)
                    || (index5 >= -1))
            {
            String s3 = s2.substring(index1);
            System.out.println(s3);
            
            }
            
        }
    }
    I'm still a bit confused on how to make charAt worthwhile here...the length doesn't work if anything comes after cat so its no good. I don't understand how to make the charAt do something I want to.

    I can get the original string down to a substring with cat, dog, or llama...but I don't know what good charAt is to me. I want to look for spaces I suppose?
  24. #28
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    You don't have to use charAt(). substring will probably do all you need.
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    i am a cat

    My substring method will get me to cat which is good.

    category is a word.

    The substring in my method will get me the entire string. This is where I get stuck. If I do tests for length, the end up not working if cat is not the last word. I get run time errors with charAt when cat ends a sentence. I can truncate a word like category into cat, but that does not help me. I also tried testing my substrings against contentEquals, but that didn't work either. What can I do with substrings that will allow me to test for differences?
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    The length you are interested in is that of cat
    You want to look at the character in front of or following cat.
    Take a piece of paper and write down several sentences with "cat" in it.
    Then write under each character in the sentence its position in the sentence starting at 0.
    Then consider what indexOf() returns and where the character after "cat" is relative to the value returned by indexOf(). It should be exacty the length of cat (3) characters later.


    Most of the problems you describe are because you haven't used a piece of paper and pencil to work out where a String is and what index values will work relative to that location.
    For example if indexOf() returns a 0, there won't be a character in front of it.
    Last edited by NormR; February 11th, 2013 at 03:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo