Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: help to find the error of coding

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default help to find the error of coding

    i cannot find out the error in output
    Input : arunjib
    required output : ArunjiB
    output getting : ABarunjib
    please help me

    public class caps
    {
        public static void func(String x)
        {
    int i, len;
    char y;
    String z, x1;
    z=" ";
    x1=" ";
    len=x.length();
    for(i=0;i<len;i++)
    {
    y=x.charAt(i);
    if(i==0 || i==len-1)
    {
    z=Character.toString(y);  // converting character to string
    x1=z.toUpperCase();   // converting string to upper case
    x=x1+x;
    }
    }
    System.out.println(x);
    }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: help to find the error of coding

    1. You are making it too complex. This can be solved in simple three lines.
    2. As far as i came to know, you want to uppercase the first and last letter of the string. So, why don't you break the string with the parts, except first and the last letter?
    3. Then process the first and last letter and concatenate them.

    Well, coming to your program now,
    x=x1+x;
    See this line?
    x1 is your first or last letter in upper case. And you are concatenating it to the original string.Let's see
    x = "java";
    when i==0
    x1=J and you will do,
    x = x1+x;
    means,
    x="Jjava";


    Now when i = len-1,
    x1=A
    and
    x=x1+x will do
    x="JAjava"
    The reason is simple, i guess you can see it now.

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    arunjib (November 27th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help to find the error of coding

    i can understand why the output comming like this. but i am unable to solve the problem. please help me

  5. #4
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help to find the error of coding

    i can understand why the output comming like this. but i am unable to solve the problem. please help me

    Quote Originally Posted by Mr.777 View Post
    1. You are making it too complex. This can be solved in simple three lines.
    2. As far as i came to know, you want to uppercase the first and last letter of the string. So, why don't you break the string with the parts, except first and the last letter?
    3. Then process the first and last letter and concatenate them.

    Well, coming to your program now,
    x=x1+x;
    See this line?
    x1 is your first or last letter in upper case. And you are concatenating it to the original string.Let's see

    The reason is simple, i guess you can see it now.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help to find the error of coding

    Try debugging your program by adding printlns that show the values of all the variables as they change and are used. When you see the printout it should help you understand what is wrong with your logic.
    When you print out a variable be sure to show its name:
    System.out.println("x=" + x);

  7. #6
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help to find the error of coding

    Though i can understand the error, i cannot solve. if anybody solve it, i would be helpfull.

    Quote Originally Posted by Norm View Post
    Try debugging your program by adding printlns that show the values of all the variables as they change and are used. When you see the printout it should help you understand what is wrong with your logic.
    When you print out a variable be sure to show its name:
    System.out.println("x=" + x);

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help to find the error of coding

    It's up to you to find the problem and change the code.
    Try debugging your program by adding printlns that show the values of all the variables as they change and are used. When you see the printout it should help you understand what is wrong with your logic.
    When you print out a variable be sure to show its name:
    System.out.println("x=" + x);

    If you print out the values of the variables you will see what the problem is. You need to see what your code is doing if you expect to be able to fix it. Printlns will help!!!

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: help to find the error of coding

    I would throw the code away and start again.

    Get first char, get last char, get all chars in between. Methods in the String class can do this
    Convert first char to uppercase, convert last char to uppercase. Methods of the Character class can do this.
    Concatenate the three parts together.

    Done and a lot simpler than OPs mangled code.
    Improving the world one idiot at a time!

  10. #9
    Junior Member
    Join Date
    Mar 2011
    Location
    West Bengal, India
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help to find the error of coding

    Very useful suggestion. thnx

    Quote Originally Posted by Mr.777 View Post
    1. You are making it too complex. This can be solved in simple three lines.
    2. As far as i came to know, you want to uppercase the first and last letter of the string. So, why don't you break the string with the parts, except first and the last letter?
    3. Then process the first and last letter and concatenate them.

Similar Threads

  1. [SOLVED] cannot find symbol error
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 08:57 AM
  2. [SOLVED] Please help with this... I think I have a minor coding error
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:02 AM
  3. can any one plz find the error.
    By shalini_sns in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2011, 05:26 PM
  4. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM
  5. Coding error
    By chemy G in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2010, 08:36 AM