Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find any word in a sentence

Tags:

java

First post and all that. I'm a novice programmer.

Anyway, I was tasked with created a program in Java that will take in user input in the form of a sentence AND a word. The program removes spaces from the sentence, and checks if the word is present in the "blank-less" sentence. However, the program also removes a letter from the end of the word, and checks if that word is present in the blank-less sentence. The program continues to remove letters from the word until there are no more letters to remove.

In addition, the program is also supposed to state the location of the word but it cannot list one location multiple times. If the program cannot find the whole word, it prints out "'Word' was not found." If it is, it prints out "'Word' was found at location 'x'"

e.g. If my sentence is "She sings by the river" and by word is "byte", the code is supposed to check "shesingsbytheriver" for "byte", "byt", "by", and "b", but it CANNOT find "byt", "by", and "b" in the same location.

Below is the code I have. Everything is fine up until my if statement. Instead of finding the word in the blank-less sentence, it continues to print out "'Word' was not found."

A few last notes: I should avoid arrays, and most of the commands I need are in String class.

Thanks!

// The purpose of this program is to take in user input in the form
// of a sentence and a word. The program repeats the sentence and word
// back, removes the spaces, and checks if the word was present in the
// sentence. The program removes a letter from the word, checks if that
// "word" is present and continues until it cannot remove any more letters.

import java.util.*;
import javax.swing.JOptionPane;

public class Program1 {

    public static void main(String[] args) {
        String sentenceBlankless;

        String sentence = JOptionPane.showInputDialog("Please enter a sentence: ");
        String word = JOptionPane.showInputDialog("Please enter a word: ");

        sentenceBlankless = sentence.replaceAll(" ", "");

        JOptionPane.showMessageDialog(null, "The original imput is: " + sentence);
        JOptionPane.showMessageDialog(null, "Removing blanks - " + sentenceBlankless);
        JOptionPane.showMessageDialog(null, "Input word - " + word);

        for (int x = 0; x < word.length(); x++) {

            if (sentenceBlankless.toLowerCase().contains(word.toLowerCase())) {
                int loc = sentence.toLowerCase().indexOf(word.toLowerCase());
                JOptionPane.showMessageDialog(null, word.substring(0, word.length() - x) + " was found at location " + loc);
            } else {
                JOptionPane.showMessageDialog(null, word.substring(0, word.length() - x) + " was not found");
            }
        }
    }
}
like image 690
Brandon S Avatar asked Jan 25 '16 04:01

Brandon S


People also ask

How do I find a word in a sentence?

Hold the Ctrl keyboard key and press the F keyboard key (Ctrl+F) or right-click (click the right mouse button) somewhere on the article and select Find (in this article).

What are the 7 types of sentences?

Answer: There are 8-types of sentences on the basis of function and structure are Declarative Sentence, Interrogative Sentence, Exclamatory Sentence, Imperative Sentence, Simple sentence, Compound Sentence, Complex sentence, and Compound -Complex sentence.


2 Answers

Your problem is that it's always searching if "byte" was found, and not substrings of byte.

This happens on this line

if (sentenceBlankless.toLowerCase().contains(word.toLowerCase())) {

You always use word which is always "byte", it's never updated.

So you can replace it with

if (sentenceBlankless.toLowerCase().contains(word.substring(0, word.length() - x).toLowerCase()))

but I wouldn't recommend it. Instead try updating word on every iteration of the for loop.

So you can do:

word = word.substring(0, word.length() - x);

Your final for loop would be:

for (int x = 0; x < word.length(); x++)
{
    word = word.substring(0, word.length() - x);
    if (sentenceBlankless.toLowerCase().contains(word.toLowerCase()))
    {
        int loc = sentenceBlankless.toLowerCase().indexOf(word.toLowerCase());
        JOptionPane.showMessageDialog(null, word + " was found at location " + loc);
    }
    else
        JOptionPane.showMessageDialog(null, word + " was not found");

}

Everything else can stay unchanged.

Offtopic:

Inside the if statement loc uses sentence instead of sentenceBlankless to get the location.

like image 132
yehyaawad Avatar answered Sep 19 '22 13:09

yehyaawad


A few comments:

The statement if (sentenceBlankless.toLowerCase().contains(word.toLowerCase())) doesn't take into account that you are looking for a shorter version of word each time. Instead of just showing a shortened version to the user in your dialog box, you could instead chop the word shorter on each iteration:

word = word.substring(0, word.length() - x)

But that will break your for loop.

Which leads me to comment #2. You could just as easily to this as a while loop:

while(word.length() > 0)

As for not being able to find the same word at the same location multiple times, that's a little bit trickier. Probably the easiest way to do it would be with an array of booleans, with each array element representing an index in the string:

boolean[] foundPositions = new boolean[sentenceBlankless.length];

Handily the boolean primitive defaults to false. So each time you find the instance of the word substring at a position, you set the flag in your array.

Then you need to check the array each time you search for the word, and if it has already been found, try the search again but start from position this time. If you have covered recursion already, this is a good time to practice it. This is some pseudo code (since I'm not going to do your homework completely) that shows how I would approach this:

private int FindPosition(string sentence, string word)
{
    find the index of word in sentence

    if not found
        return -1

    // We know sentence contains word
    if not previously found
        return found index

    // We know it was found, but has been found before, let's try again
    // but cut out the where it was found last time
    // This is recursion (calling a method from within the method)
    return FindPosition(sentence.substring, word)
}

Be aware of updating the flag array, and the specific substring you need to pass back in the recursive method.

like image 20
Ian Avatar answered Sep 17 '22 13:09

Ian