Friday, January 28, 2011

BlueJ Program Displaying An Entered Full Name In Short Form



This is BlueJ program on string modification. User  entered name with middle name and surname which is stored in a string object will be displayed in short form. Initial alphabets of the name and middle name(s) and the complete surname will be displayed. The full name may have number of middle names. The initial alphabets of name, middle name and surname will be in capital letter and the rest of alphabets will be in lower case.

How to proceed on this string modification program

We have to recognize the initial alphabets of name, middlename and surname the entered name. The first alphabet of the string is the initial alphabet of the name and accessing it is not a tough job. Next initial alphabets should be just next to blank space as after name, middlename and surname there should be blank space. If user enters multiple consecutive blank spaces, it can be removed also.

So, in this BlueJ program we need to access the blank spaces from the string and the alphabet following the blank spaces are to be taken which will be the initial alphabet of either name, middlename or surname. If these initial alphabets are in lower case, they should be converted in to upper case. All the alphabets of the last word means the surname should be taken and the alphabets excepting the first one should be changed to lower case if required.

Codes of the string modification program

import java.io.*;
class Words2
{
 int sp;
 BufferedReader br;
 String text,text1="";
  public static void main(String args[])throws IOException
   {
  Words2 ob=new Words2();
 ob.accept();
 ob.result();
  }
  Words2()
  {
   br=new BufferedReader(new InputStreamReader(System.in));
   text="";
   sp=0;
  }
public void accept()throws IOException
{
 System.out.println("Enter the Name:");
 text=br.readLine().trim();
}
public void result()
{
int i=0,len,flag=0;
char ch;
System.out.println("The entered Name ="+text);
 len=text.length();
 do
 {
  if(text.charAt(i)==' ')
  sp++;
  i++;
 }while(i< len);
i=0;
do
{
ch=text.charAt(i);
 if(i==0)
 {
 if(ch >=97 && ch< =122)
 ch=(char)(ch-32);
 text1=text1+ch;
 }
 else if(ch==' ')
 {
  flag=1;
  text1=text1+".";
  sp--;
 }
 else if(flag==1)
 {
 if(ch >=97 && ch< =122)
 ch=(char)(ch-32);
 text1=text1+ch;
 flag=0;
 }
 else if(sp==0)
 {
 if(ch >=65 && ch< =90)
 ch=(char)(ch+32);
 text1=text1+ch;
 }
i++;
}while(i< len);
text1=text1.trim();
System.out.println("Name in short form="+text1);
}
}

Technical analysis of the string modification program

Blank spaces of the entered string is counted first using a do while loop in this program. Again each apphabets are accessed using another do while loop where the required modification is done. The first alphabet of the string acceesed is the initial character of the name and the character is converted into upper case is necessary and concatenated with another string object. Whenever any blank space in the string is encountered, the variable containing the number of blank spaces in the string is decreased by 1, a dot (.) is concatenated with the string object containing the modified string and another variable ‘flag’ is set to 1. This value indicates that blank space is encountered just one character before. So the alphabets when the value of flag is 1, is concatenated to the modified string object. These alphabets are those initial alphabets of middlename and surname. When the value of the variable holding the number of blank spaces becomes zero, it indicates that surname alphabets excepting the initial character is accessed, so they are converted in to lower case if required and attached to the modified string object.





21 comments:

  1. This program will give the full name shortform whereas i wanted the last name to be present.
    Eg:
    Input: Rajkumar Manmohan Bhatt.
    Output:
    R.M.Bhatt

    ReplyDelete
    Replies
    1. Maybe this program will help you.
      import java.io.*;
      class name
      {
      BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
      void check()throws IOException
      {
      int pos=0,pos1=0;
      System.out.println("Enter a name with first, middle and surname:");
      String str=inp.readLine();
      int l=str.length();
      System.out.print(str.charAt(0)+".");
      for(int i=0;i<l;i++)
      {
      char ch=str.charAt(i);
      if(ch==' ')
      {
      pos=i+1;
      System.out.print(str.charAt(pos)+".");
      break;
      }
      }
      for(int i=pos;i<l;i++)
      {
      char ch1=str.charAt(i);
      if(ch1==' ')
      {
      pos1=i;
      }
      }
      for(int i=pos1;i<l;i++)
      {
      System.out.print(str.charAt(i));
      }
      }
      }

      Delete
    2. It works fine for a name middle name and surname but if a person do not have a middle name or more than one middle name?

      Delete
    3. it's fine but you forgot to create the main method hence it could not work...

      Delete
    4. The program will run in BlueJ editor.

      Delete
  2. Have you executed the program?

    ReplyDelete
  3. It shows an error..@ while(i... and says ')' expected.

    ReplyDelete
    Replies
    1. no the program is working just the gaps in < = should not be there it should be like <=...........
      else it is giving the correct output after that correction

      Delete
    2. You are correct. Actually if I write like i<a, it sometimes gives html error, that's why the gap is given.

      Delete
  4. Just now I rechecked the program on displaying name in short form and it's running. Please recheck.

    ReplyDelete
  5. Sir, this program is not being executed.

    ReplyDelete
  6. Sir, could you please explain the function of trim()

    ReplyDelete
  7. Please refer my post on String class functions

    ReplyDelete
  8. Anonymous - posted on February 19, 11.pm. Please refer the program which is not running.

    ReplyDelete
  9. sir,can u give the solution to this prg on combinations,like if u enter 5 then the output shud be 1 1 1 1 1
    1 1 1 2
    1 1 3
    1 4
    2 3
    (all the diff combinations for the entered number shud be printed in a pattern like this)

    ReplyDelete
  10. sir,how to merge two string arrays.give me an example??

    ReplyDelete
  11. For the pattern 11111
    1112
    .....

    Check my post on 'different combination of digits'.

    ReplyDelete
  12. Sir, im an ISC student. I have got my practical Tomorrow. I have checked out almost all your programs. Could you pls suggest me some other types of string programs that might come? Also, number, date programs would do. Anything except arrays.
    Thanks.

    ReplyDelete
    Replies
    1. In the last moment, go through different type of string manipulating programs. One program on string will definitely come.

      Delete
  13. thnx a lot.... It really helped me

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner