Word hunt

Member #531174 0 Tallied Votes 260 Views Share

The program accepts a sentence, stores each word in it as a separate element in an array, then asks you for a word, and return whether or not the word is present in the sentence you entered.

import java.io.*;
class search_arrays
{
static void search()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a sentence: ");
        String s=br.readLine();
        s+=" ";
        String[] x=new String[s.length()];
        int a=0,b=0;
        for(int i=0;i<x.length;i++)
            {
                if(s.charAt(i)==' ')
                    {
                        x[a]=s.substring(b,i);
                        a++;
                        b=i+1;
                    }
                }
         System.out.println("\nEnter a word you want to search for: ");
         s=br.readLine();
         boolean ans=false;
         for(int i=0;i<x.length;i++)
            {
                if(s.equalsIgnoreCase(x[i]))
                    {
                        ans=true;
                    }
                }
          if(ans)
            {
                System.out.println("\n"+s+" has been entered.");
            }
          else
            {
                System.out.println(s+" has not been entered.");
            }
        }
    }

Dani AI

Generated

Nice start, . Two practical issues to address: the manual loop allocates an array sized to the sentence length (lots of nulls, inefficient) and it splits only on spaces, so trailing punctuation like "you?" won't match "you". Two simple, robust approaches follow — pick the one that fits your use case.

Use normalization + split + Set (good when you may search many times):

String normalized = sentence.replaceAll("[^\\p{L}\\p{N}'\\-]+", " ").trim();
String[] words = normalized.split("\\s+");
Set<String> wordsSet = new HashSet<>();
for (String w : words) wordsSet.add(w.toLowerCase(Locale.ROOT));
boolean found = wordsSet.contains(query.toLowerCase(Locale.ROOT));

This replaces punctuation with spaces (keeps letters, digits, apostrophes and hyphens), avoids empty tokens, and gives O(1) lookups per search.

Use a whole-word regex match (good for single ad-hoc checks):

Pattern p = Pattern.compile("\\b" + Pattern.quote(query) + "\\b",
    Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
Matcher m = p.matcher(sentence);
boolean found = m.find();

This finds the exact word even if it's followed or preceded by punctuation. Use Pattern.quote(query) to safely handle special characters in the search term.

Quick tips: trim the input first; use Locale.ROOT for predictable lowercasing; prefer meaningful class/method names and add a main method if you want a runnable example. If you expect lots of searches over the same text, build an index (HashSet) once; for one-off checks the regex approach is simpler.

Member Avatar for Member #531174
Member #531174

Oh, and I almost forgot; if you end the sentence like "...you?" and search for "you", it will not take into account the last "you" since there is a special character immediately after it.
Let's see if anyone has the solution for this! :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.