Results 1 to 3 of 3
  1. #1
    jpnym15 is offline Member
    Join Date
    Nov 2007
    Posts
    19
    Rep Power
    0

    Default I am completely stuck

    Java Code:
    public class StringSearcher
    {
       public static void main (String[] args)
       {   
       
          java.util.Scanner userInput = new java.util.Scanner(System.in);
    
        String sentence = new String();
        String searchword = new String();
        int sentenceLength;
        int wordLength;
        int wordIndex;
        int total = 0;
        int index;
    
        index = sentence.indexOf(" ");
        while(index != -1)
        {
           total++;
           index = sentence.indexOf("searchword", index + 1);
        }
    
        System.out.println("Enter a sentence.");
        sentence = userInput.nextLine();
    
        System.out.println("Now enter a search word.");
        searchword = userInput.nextLine();
      
        System.out.println("Your sentence had the search word "+searchword+"+total+" times");
       }
    }
    That is my program...how would i go about finding the searchword, and also finding how many times it is found in the sentence.
    Last edited by JavaBean; 11-14-2007 at 06:38 PM.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    jpnym15, please learn to use [code] block for your codes: http://www.java-forums.org/misc.php?do=bbcode

  3. #3
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    That is my program...how would i go about finding the searchword, and also finding how many times it is found in the sentence.
    You start searching in your while loop before getting input from the user. Try getting the user first and then searching..

Similar Threads

  1. musically stuck cry for help 2
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 02:09 PM
  2. musically stuck
    By geork in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 09:44 PM
  3. Stuck and Frustrated.
    By jazzinspace in forum New To Java
    Replies: 7
    Last Post: 01-12-2008, 02:38 PM
  4. Files not completely deleted on Linux
    By amamare in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 02:12 PM
  5. Replies: 3
    Last Post: 12-05-2007, 02:22 AM

Posting Permissions

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