Reputation: 618
The following is a problem from codingbat.
Given a string, return a string where for every char in the original, there are two chars. e.g.:
doubleChar("The") → "TThhee" doubleChar("AAbb") → "AAAAbbbb" doubleChar("Hi-There") → "HHii--TThheerree"
I have two statements that can do this, but the statement in the comment doesn't give the excepted output:
public String doubleChar(String str) {
String str1 = "";
for(int i=0;i<str.length();i++)
{
//str1 += str.charAt(i) + str.charAt(i);
str1 += str.substring(i,i+1)+str.substring(i,i+1);
}
return str1;
}
If I change the commented part to str1 = str1 + str.charAt(i) + str.charAt(i)
, the output is as required. I am not able to understand this. If the concatenation doesn't, then it shouldn't work for either of the case. Can you help me in this?
Upvotes: 2
Views: 2291
Reputation: 9
public String doubleChar(String str) {
String result = "";
for ( int i=0;i<str.length();i++){
String ww = str.substring(i, i+1);
result += ww + ww;
}
return result;
}
Upvotes: 0
Reputation: 6647
The problem is that you are trying to add two chars, which behaves differently to adding two strings. All you need to do is save the char as a string and you can add them the way you want.
public String doubleChar(String str) {
String str1 = "";
for(int i=0;i<str.length();i++)
{
String currentChar = Character.toString(str.charAt(i);
str1 += currentChar + currentChar;
}
return str1;
}
Upvotes: 0
Reputation: 86
public String doubleChar(String str) {
String newStr=""; // created variable to store our result.
for(int i=0;i<str.length();i++){ // loop through each char in the string.
char add = str.charAt(i); // take out single char from the string and store it into am variable.
newStr+=""+add+add; // add each character
}
return newStr;
}
Upvotes: 1
Reputation: 11132
The method public char charAt(int index)
returns a char
, so the line
str1 += str.charAt(i) + str.charAt(i);
performs the +
operator on two char
s.
According to the Java Specs:
15.18. Additive Operators
The operators + and - are called the additive operators.
AdditiveExpression: MultiplicativeExpression AdditiveExpression + MultiplicativeExpression AdditiveExpression - MultiplicativeExpression
The additive operators have the same precedence and are syntactically left-associative (they group left-to-right).
If the type of either operand of a + operator is
String
, then the operation is string concatenation.Otherwise, the type of each of the operands of the + operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.
In every case, the type of each of the operands of the binary - operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.
so a char +
a char is addition, not concatenation.
Every char
is actually an integer value that is interpreted as an character.
Section 15.15.3 of the Java Specs says:
15.15.3. Unary Plus Operator +
The type of the operand expression of the unary + operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.
Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.
At run time, the value of the unary plus expression is the promoted value of the operand.
Therefore, adding two char
s produces an int
. When you +=
that int
, it is converted to a String
concatenated to said string.
To fix this problem, you can use any of the answers to this question: How to convert a char to a String?, e.g.
str1 += String.valueOf(str.charAt(i)) + str.charAt(i);
or
str1 += "" + str.charAt(i) + str.charAt(i);
Upvotes: 6
Reputation: 77044
str.charAt(i)
returns a char
, adding two char
s results in a char
with a codepoint equal to the sum of the input codepoints. When you start with str +
, the first concatenation is between a String
and a char
, which results in a String
, followed by the second concatenation, also between a String
and a char
.
You can fix this a few ways, such as:
str1 += String.valueOf(str.charAt(i)) + str.charAt(i);
or
str1 += "" + str.charAt(i) + str.charAt(i);
or, as you've already discovered, and likely the most readable:
str1 = str1 + str.charAt(i) + str.charAt(i);
Upvotes: 7