Start New Discussion within our Software Development Community

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.");
            }
        }
    }

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

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.