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
Code :
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);
}
}
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,
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
Quote:
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.
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
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
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,
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.
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);
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
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);
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!!!
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.
Re: help to find the error of coding
Very useful suggestion. thnx
Quote:
Originally Posted by
Mr.777
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.