?
Solved

Help with Java Code

Posted on 2011-04-25
5
Medium Priority
?
305 Views
Last Modified: 2012-08-14
Hello all,

I have a simple java program that accepts user input of a sentence and a word, and reports the number of times the given word appears in the given sentence. It works great, except in one strange, but funny case.

If I type:

the dog is the bomb yo

and search for the word the, I get the result of two (2) because 'the' appears twice.

However, if I type:

the dog is not the bomb yo

and search for the word the, I get the result of one (1).

It's almost like the program is ignoring the second 'the' because the word 'not' is in front of it.

Here is my code:

 
import java.io.*;

public class SentenceSearch 
{ // SentenceSearch()
	
	/** Class Variables **/
	
	public String sentence;
	public String word;
	
	/** Constructors **/
	
	public SentenceSearch()
	{
		// empty constructor
	}
	
	public SentenceSearch(String newSentence, String newWord)
	{
		sentence = newSentence;
		word = newWord;
	}
	
	/** Accessors/Mutators **/
	
	public void acceptInput() throws IOException
	{
		// allow user input
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(ir);
		
		// prompt user for sentence
		System.out.println("Enter the sentence to search:");
		sentence=br.readLine();
		
		// prompt user for word
		System.out.println("Enter the word to search for:");
		word=br.readLine();
		
		// begin searchCount()
		searchCount();
	}
	
	public void searchCount() throws IOException
	{
		int count=0, num=0;
		
		// start searching for number of times the word appears
		try{
&#9;&#9;&#9;for(int i=0;i<sentence.length();i++)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;if(word.charAt(0)==sentence.charAt(i))
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;for(int j=0;j<word.length();j++,i++)
&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;if(sentence.charAt(i)==word.charAt(j))
&#9;&#9;&#9;&#9;&#9;&#9;{ count=count+1;}
&#9;&#9;&#9;&#9;&#9;&#9;if(count==word.length())
&#9;&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;&#9;num=num+1;count=0;
&#9;&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;}
&#9;&#9;}catch(Exception e){}
&#9;&#9;
&#9;&#9;// pass the number to findWord to report 
&#9;&#9;// number of times a word appears
&#9;&#9;findWord(num);
&#9;}
&#9;
&#9;public void findWord(int num)
&#9;{
&#9;&#9;if(num==0) // if word was not found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Sorry, your word is not present in this sentence.");
&#9;&#9;}
&#9;&#9;else // if word was found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Your word is present in the " +
&#9;&#9;&#9;&#9;&#9;"sentence "+num+" time(s)");
&#9;&#9;}
&#9;}
&#9;
&#9;public static void main(String args[]) throws IOException
&#9;{ // main()
&#9;&#9;
&#9;&#9;// tell the user what's going on
&#9;&#9;System.out.println("******* TEST SENTENCE SEARCH *******");
&#9;&#9;System.out.println(" ");
&#9;&#9;System.out.println("Sentence to test: the bird is the word.");
&#9;&#9;System.out.println("  Word to locate: the");
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start the test
&#9;&#9;SentenceSearch test1 = new SentenceSearch("the bird is the word.", "the");
&#9;&#9;test1.searchCount();
&#9;&#9;
&#9;&#9;// extra space for readability
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start a new test
&#9;&#9;SentenceSearch test2 = new SentenceSearch();
&#9;&#9;test2.acceptInput();
&#9;}
} // SentenceSearch()

Open in new window


How do I fix that problem?

Hopefully the code didn't lose too much formatting from being pasted here. Also, let me know if you think that there is a better way to do this and/or if there is redundant code.

Thanks,

Kody
0
Comment
Question by:Kody-Burg
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Justin Mathews earned 2000 total points
ID: 35462251
There is a flaw in your logic. In the searchCount() method inner loop i should be incremented only if the characters match as below:


import java.io.*;

public class SentenceSearch 
{ // SentenceSearch()
&#9;
&#9;/** Class Variables **/
&#9;
&#9;public String sentence;
&#9;public String word;
&#9;
&#9;/** Constructors **/
&#9;
&#9;public SentenceSearch()
&#9;{
&#9;&#9;// empty constructor
&#9;}
&#9;
&#9;public SentenceSearch(String newSentence, String newWord)
&#9;{
&#9;&#9;sentence = newSentence;
&#9;&#9;word = newWord;
&#9;}
&#9;
&#9;/** Accessors/Mutators **/
&#9;
&#9;public void acceptInput() throws IOException
&#9;{
&#9;&#9;// allow user input
&#9;&#9;InputStreamReader ir = new InputStreamReader(System.in);
&#9;&#9;BufferedReader br = new BufferedReader(ir);
&#9;&#9;
&#9;&#9;// prompt user for sentence
&#9;&#9;System.out.println("Enter the sentence to search:");
&#9;&#9;sentence=br.readLine();
&#9;&#9;
&#9;&#9;// prompt user for word
&#9;&#9;System.out.println("Enter the word to search for:");
&#9;&#9;word=br.readLine();
&#9;&#9;
&#9;&#9;// begin searchCount()
&#9;&#9;searchCount();
&#9;}
&#9;
&#9;public void searchCount() throws IOException
&#9;{
&#9;&#9;int count=0, num=0;
&#9;&#9;
&#9;&#9;// start searching for number of times the word appears
&#9;&#9;try{
&#9;&#9;&#9;for(int i=0;i<sentence.length();i++)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;if(word.charAt(0)==sentence.charAt(i))
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;for(int j=0;j<word.length();j++)
&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;if(sentence.charAt(i)==word.charAt(j))
&#9;&#9;&#9;&#9;&#9;&#9;{ count=count+1; i++;}
&#9;&#9;&#9;&#9;&#9;&#9;if(count==word.length())
&#9;&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;&#9;num=num+1;count=0;
&#9;&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;}
&#9;&#9;}catch(Exception e){}
&#9;&#9;
&#9;&#9;// pass the number to findWord to report 
&#9;&#9;// number of times a word appears
&#9;&#9;findWord(num);
&#9;}
&#9;
&#9;public void findWord(int num)
&#9;{
&#9;&#9;if(num==0) // if word was not found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Sorry, your word is not present in this sentence.");
&#9;&#9;}
&#9;&#9;else // if word was found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Your word is present in the " +
&#9;&#9;&#9;&#9;&#9;"sentence "+num+" time(s)");
&#9;&#9;}
&#9;}
&#9;
&#9;public static void main(String args[]) throws IOException
&#9;{ // main()
&#9;&#9;
&#9;&#9;// tell the user what's going on
&#9;&#9;System.out.println("******* TEST SENTENCE SEARCH *******");
&#9;&#9;System.out.println(" ");
&#9;&#9;System.out.println("Sentence to test: the bird is the word.");
&#9;&#9;System.out.println("  Word to locate: the");
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start the test
&#9;&#9;SentenceSearch test1 = new SentenceSearch("the bird is the word.", "the");
&#9;&#9;test1.searchCount();
&#9;&#9;
&#9;&#9;// extra space for readability
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start a new test
&#9;&#9;SentenceSearch test2 = new SentenceSearch();
&#9;&#9;test2.acceptInput();
&#9;}
} // SentenceSearch()

Open in new window

0
 
LVL 6

Author Closing Comment

by:Kody-Burg
ID: 35462357
Thank you! Sometimes code just needs a second pair of eyes to look it over. :)
0
 
LVL 6

Author Comment

by:Kody-Burg
ID: 35462522
Just a quick additional question for you. If I input the following sentence:

The theory of magic states that Theodore is the other mage's brother.

which should return a result of 4. Instead it returns a result of 5. Do you know how I can fix that?

Thanks.
0
 
LVL 6

Author Comment

by:Kody-Burg
ID: 35462528
when I type the word "the"
0
 
LVL 16

Expert Comment

by:Justin Mathews
ID: 35462685
If you are not particular about matching character by character, you could use the indexOf() method. Below is a refined version of the previous logic:
import java.io.*;

public class SentenceSearch 
{ // SentenceSearch()
&#9;
&#9;/** Class Variables **/
&#9;
&#9;public String sentence;
&#9;public String word;
&#9;
&#9;/** Constructors **/
&#9;
&#9;public SentenceSearch()
&#9;{
&#9;&#9;// empty constructor
&#9;}
&#9;
&#9;public SentenceSearch(String newSentence, String newWord)
&#9;{
&#9;&#9;sentence = newSentence;
&#9;&#9;word = newWord;
&#9;}
&#9;
&#9;/** Accessors/Mutators **/
&#9;
&#9;public void acceptInput() throws IOException
&#9;{
&#9;&#9;// allow user input
&#9;&#9;InputStreamReader ir = new InputStreamReader(System.in);
&#9;&#9;BufferedReader br = new BufferedReader(ir);
&#9;&#9;
&#9;&#9;// prompt user for sentence
&#9;&#9;System.out.println("Enter the sentence to search:");
&#9;&#9;sentence=br.readLine();
&#9;&#9;
&#9;&#9;// prompt user for word
&#9;&#9;System.out.println("Enter the word to search for:");
&#9;&#9;word=br.readLine();
&#9;&#9;
&#9;&#9;// begin searchCount()
&#9;&#9;searchCount();
&#9;}
&#9;
&#9;public void searchCount() throws IOException
&#9;{
&#9;&#9;int count=0, num=0;
&#9;&#9;
&#9;&#9;// start searching for number of times the word appears
&#9;&#9;try{
&#9;&#9;&#9;for(int i=0;i<sentence.length();i++)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;if(word.charAt(0)==sentence.charAt(i))
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;for(int j=0;j<word.length();j++, i++)
&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;if(sentence.charAt(i)!=word.charAt(j))
&#9;&#9;&#9;&#9;&#9;&#9;&#9;break;
&#9;&#9;&#9;&#9;&#9;&#9;count++;
&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;&#9;if (count==word.length())
&#9;&#9;&#9;&#9;&#9;&#9;num++;
&#9;&#9;&#9;&#9;&#9;count = 0;
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;}
&#9;&#9;}catch(Exception e){}
&#9;&#9;
&#9;&#9;// pass the number to findWord to report 
&#9;&#9;// number of times a word appears
&#9;&#9;findWord(num);
&#9;}
&#9;
&#9;public void findWord(int num)
&#9;{
&#9;&#9;if(num==0) // if word was not found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Sorry, your word is not present in this sentence.");
&#9;&#9;}
&#9;&#9;else // if word was found
&#9;&#9;{
&#9;&#9;&#9;System.out.println("Your word is present in the " +
&#9;&#9;&#9;&#9;&#9;"sentence "+num+" time(s)");
&#9;&#9;}
&#9;}
&#9;
&#9;public static void main(String args[]) throws IOException
&#9;{ // main()
&#9;&#9;
&#9;&#9;// tell the user what's going on
&#9;&#9;System.out.println("******* TEST SENTENCE SEARCH *******");
&#9;&#9;System.out.println(" ");
&#9;&#9;System.out.println("Sentence to test: the bird is the word.");
&#9;&#9;System.out.println("  Word to locate: the");
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start the test
&#9;&#9;SentenceSearch test1 = new SentenceSearch("the bird is the word.", "the");
&#9;&#9;test1.searchCount();
&#9;&#9;
&#9;&#9;// extra space for readability
&#9;&#9;System.out.println(" ");
&#9;&#9;
&#9;&#9;// start a new test
&#9;&#9;SentenceSearch test2 = new SentenceSearch();
&#9;&#9;test2.acceptInput();
&#9;}
} // SentenceSearch()

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Simple Linear Regression
Progress
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question