You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
have access to post topics
communicate privately with other members (PM)
not see advertisements between posts
have the possibility to earn one of our surprises if you are an active member
access many other special features that will be introduced later.
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.