Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By ozzyman

Thread: A java program to print short form of a name.

  1. #1
    Neeer is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Post A java program to print short form of a name.

    import java.io.*;
    public class Netaji
    {
    public static void main()throws IOException
    {
    InputStreamReader reader=new InputStreamReader(System.in);
    BufferedReader input=new BufferedReader(reader);
    System.out.println("Enter your name please");
    String a=input.readLine();
    int count=a.length();
    int i=1,adprint;
    char pchar,hold;

    for(i=1;i<=count;i++)
    {
    hold=a.charAt(i);
    if(i==1)
    {
    pchar=a.charAt(0);
    System.out.print(pchar+".");
    }

    else if(hold==' ')
    {
    adprint=i+1;
    pchar=a.charAt(adprint);
    System.out.print(pchar+".");
    }
    }

    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Did you have a question, or is this just an exhibition of your l33t sk1llz?

    Recommended reading:
    Code Conventions for the Java(TM) Programming Language: Contents
    How To Ask Questions The Smart Way

    And use code tags to post code.

    db

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    int i=1,adprint;
    for(i=1;i<=count;i++)

    you dont need to declare 'int i=1' before a for-loop. you can do that inside the loop e.g.
    Java Code:
    for (int i=1; i<=count; i++)

    and you should move this to the Java Tips section :P
    diplesh likes this.

  4. #4
    diplesh is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default Re: A java program to print short form of a name.

    Quote Originally Posted by ozzyman View Post
    you dont need to declare 'int i=1' before a for-loop. you can do that inside the loop e.g.
    Java Code:
    for (int i=1; i<=count; i++)

    and you should move this to the Java Tips section :P
    //JAVA CODE FOR DISPLAYING NAME AS IN SHORT FORM.

    import java.io.*;
    import java.util.*;
    class SortName
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


    System.out.print("\nEnter your Fisrt name:\t");
    char first=br.readLine().charAt(0);
    br.skip(2);
    System.out.print("\nEnter middle Name:\t");
    char mid=br.readLine().charAt(0);
    br.skip(2);
    System.out.print("\nEnter your last name:\t");
    String last=br.readLine();
    System.out.println("\nshort name:\t"+first+"."+mid+"."+last);

    }
    }

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: A java program to print short form of a name.

    Your code will crash when you don't actually have a middle name, and you posted in a thread that is over three years old.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: A java program to print short form of a name.

    ... and ignored the advice in #2.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. short message service (SMS in java)
    By ashin in forum Advanced Java
    Replies: 4
    Last Post: 06-04-2011, 08:59 AM
  2. Replies: 3
    Last Post: 02-20-2011, 01:15 PM
  3. Very newbie trying to run form program
    By projecttoday in forum New To Java
    Replies: 2
    Last Post: 01-12-2011, 07:28 AM
  4. Replies: 1
    Last Post: 05-27-2010, 10:48 PM
  5. how to print out lines in a table- like form?
    By helloworld in forum New To Java
    Replies: 5
    Last Post: 02-10-2009, 10:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •