Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old February 10th, 2013, 03:27 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #17  
Old February 10th, 2013, 03:40 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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.

Reply With Quote
  #18  
Old February 10th, 2013, 03:55 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #19  
Old February 10th, 2013, 05:37 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
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.

Reply With Quote
  #20  
Old February 10th, 2013, 05:50 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
I'm suggesting that you look at the character after the "t" after you have found "cat"

Quote:
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.

Reply With Quote
  #21  
Old February 10th, 2013, 07:23 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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?

Reply With Quote
  #22  
Old February 10th, 2013, 07:40 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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?


Quote:
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.

Reply With Quote
  #23  
Old February 11th, 2013, 01:20 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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.

Reply With Quote
  #24  
Old February 11th, 2013, 01:57 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
The = operator is for assignment. An assignment statement returns the value of the left hand operand.
The == operator is for testing equality.

Reply With Quote
  #25  
Old February 11th, 2013, 02:14 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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.

Reply With Quote
  #26  
Old February 11th, 2013, 02:17 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
  #27  
Old February 11th, 2013, 02:43 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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?

Reply With Quote
  #28  
Old February 11th, 2013, 03:13 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
You don't have to use charAt(). substring will probably do all you need.

Reply With Quote
  #29  
Old February 11th, 2013, 03:21 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
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?

Reply With Quote
  #30  
Old February 11th, 2013, 03:28 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Filtering Strings

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap