Results 1 to 7 of 7
  1. #1
    blackrabbit is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default Finding a word in a sentence?

    Hi, I'm very new to java and am a bit lost really.

    I'm working with a String called 'sentence', which equals "the quick brown fox jumped over the lazy dog."

    How can I query this, asking if the sentence includes the word "fox", printing the result as "Yes", or "No if "fox isn't part of the sentence? Would this have to be done in an array, or am I way off the mark?

    Thanks in advance!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You could do it in an array; I believe Java supports tokenizing a String (splitting it up into "tokens" based on a specific character as a separator). You could also do it by looping through each character in the string.

    I'd suggest looking at these:
    StringTokenizer (Java 2 Platform SE v1.4.2)
    Parsing Strings with split
    loop through a string in java - Java answers

    Good luck!

  3. #3
    bopeep123 is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default

    You can use the indexOf method of the String.

    String string = "the quick brown fox jumped over the lazy dog.";
    Boolean b = string.indexOf("fox") > 0;

    This is case-sensitive. If you want it to ignore the case just use regex.

    Hope this helps.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That would also find "abcfoxdef" in the sentence though. He'd have to then check if the string started with "fox ", if " fox " was in the sentence, or if it ended with " fox".

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    OP needs to consider what the definition of a word is. What are its delimiters?
    white spaces, punctuation, or ()s or ???
    Could have a method the took a char and returned true if the char was a valid word delimiter. Then with indexOf() finding a substring, testing for valid delimiters.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Java Code:
    		String sentence = "the quick brown fox jumped over the lazy dog.";
    		System.out.println(sentence.contains("fox") ? "yes" : "no");

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    But does the contains() method test if the found substring is a word?
    I think it only finds substrings.
    The requirements: if the sentence includes the word "fox"

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 10:34 AM
  2. Finding and storing a word from a string builder
    By ao241 in forum Advanced Java
    Replies: 3
    Last Post: 06-28-2010, 12:46 PM
  3. Replies: 4
    Last Post: 05-07-2010, 02:06 PM
  4. String/sentence to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 5
    Last Post: 11-20-2008, 03:33 PM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 05:43 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
  •