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

    Question Help .....PLEASE!

    code:

    public class T_alu_pain
    {
    public static void main()
    {String s="Mohan Karamchand Gandhi";
    s=" "+s;
    int l=s.length();
    int i,p=0;
    char c;
    for(i=0;i<l;i++)
    {
    c=s.charAt(i);
    if(c==' ')
    {
    p=i;
    }
    }
    for(i=0;i<=l;i++)
    {
    c=s.charAt(i);

    if(c==' ')
    {
    System.out.print(c+".");
    }
    for(i=p+i;i<l;i++)
    {
    System.out.print(s.charAt(i));
    }
    }
    }
    }
    Actually I want to print M.K.Gandhi but it is printing .Gandhi can you point out the problem.

  2. #2
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    please use the [ code ] , [ / code ] tags when posting code.
    It will make my job easier, and everyone else who is trying to help you.

    first off the declaration of the main method is wrong, it should be:
    Java Code:
    public static void main(String...args) or public static void main(String[] args)
    basically if you want it to print M.K.Gandhi, you would have to do something like this:
    Java Code:
    String name = "Mohan Karamchand Gandhi";
    StringBuffer newName = new StringBuffer();
    since you already know that first letter is M, you would do:
    newName.append("M.");
    than you can do a search 
    newName.append(name.charAt(name.indexOf("K")));
    and then finally:
    newName.append(".Gandhi");
    Ofcourse this is all based on fact that you only want to do this for just this String, if you want to do it for any name you would have to do some additional work.
    Last edited by f1gh; 03-25-2011 at 03:40 PM.

  3. #3
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Your Output is " . Gandhi" !

    -->

    if(c==' ')
    {
    System.out.print(c+".");
    }

    translate this into words --> if c is a blank character, then it will print c (which is a blank character!!!) plus the point ! But you want print the next character then or? :)

    -->
    if (c == ' ') {
    System.out.print(s.charAt(i+1) + ".");

    (in one line: System.out.println(s.split(" ")[0].charAt(0)+"."+s.split(" ")[1].charAt(0)+" "+s.split(" ")[2]); D - its bad, extract the split to one extra call :D )
    (or use Pattern and Matcher :D )
    Last edited by eRaaaa; 03-25-2011 at 03:44 PM.

  4. #4
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    There are numerous ways to tackle the problem:
    You could split the original string and than check if the name is 2 words or 3 words. If it's 2 words than you just take the first character of the first+"." and take the entire second word and concatenate them.

    If it's 3 words in array after split, than you can just take first letter of 1stWord+"." and first word of second word 2ndWord+"." and than join the entire third word.

    Java Code:
            String name = "King William Charles";
    	String[] splitName = name.split(" ");
    	String newName = "";
    	if(splitName.length == 2)
    	    {
    	      newName += splitName[0].charAt(0)+"."+splitName[1];
    	    }
    	if(splitName.length == 3)
    	    {
    	     newName += splitName[0].charAt(0)+"."+splitName[1].charAt(0)+"."+splitName[2];
    	   }
    	System.out.println(newName);
    Last edited by f1gh; 03-25-2011 at 04:13 PM. Reason: edit: didn't realize eRaaaa had already given same answer

Posting Permissions

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