Thursday, March 31, 2016

BlueJ Program To Find The Word With Maximum Length In A Sentence


 In this BlueJ program, user will enter a sentence a program will display the word with maximum length from the program.

  Codes of the program is given below

import java.io.*;
public class Word
{
    String st1,st2,maxSt;
    BufferedReader br =new BufferedReader (new InputStreamReader(System.in));
    int i,max=0;
 
    public Word()
    {
      
    }

   public void takeSentence() throws Exception
   {
        System.out.println("Enter the sentence:");
        st1=br.readLine();
        while(true)
        {
             i=st1.indexOf(" ");
            if (i== -1)
            break;
             st2=st1.substring(0,i);
             st1=st1.substring(i+1);
            if(st2.length() >max)
            {
                 max=st2.length();
                 maxSt=st2;
                }
            }
           if(st1.length() >max)
            {
                 maxSt=st1;
                }
                System.out.println("The word with maximum length="+maxSt);
            }
            public static void main(String args[])throws Exception
            {
                 Word ob=new Word();
                 ob.takeSentence();
                }
            }
           

Details and Technical analysis of the program


 In this program I have used 'indexOf ()' and 'substring ()' functions of String class to break the sentence into words. Function 'indexOf ()' has many overloaded versions and in this program the function taked a string value as argument and searches the string value in the invoking string object. The function returns the first location of the argument string if found and if the argument value is not present in the invoking string object it returns -1.

'substring ()' function extracts a part of the invoking string depending on the argument. The call 'st2=st1.substring(0,i);' extracts a word and the second call 'st1=st1.substring(i+1);' extracts the remaining string.

The loop ends when there is only one word and so at the end of the loop, the last word is again checked.



The same program using StringTokenizer class (For ISC students)

import java.io.*;
import java.util.*;
public class Word
{
    String st1,st2,maxSt;
    BufferedReader br =new BufferedReader (new InputStreamReader(System.in));
    int i,max=0;
    StringTokenizer st;
 
    public Word()
    {
      
    }

   public void takeSentence() throws Exception
   {
        System.out.println("Enter the sentence:");
        st1=br.readLine();
       st=new StringTokenizer (st1);
           while(st.hasMoreTokens())
           {
                st2=st.nextToken();
                if(st2.length()>max)
            {
                 maxSt=st2;
                 max=st2.length();
                }
            }
                System.out.println("The word with maximum length="+maxSt);
            }
            public static void main(String args[])throws Exception
            {
                 Word ob=new Word();
                 ob.takeSentence();
                }
            }
           

Sample input and output


Enter the sentence:
This is a test of max length test

The word with maximum length=length

Related Post: BlueJ Programs on String/Sentence

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner