This question already has answers here:
The concatenation of chars to form a string gives different results
(5 answers)
Closed 9 years ago.
I have a string str, let's say it's value is "hell".
The below statement returns "205hellhe" instead of "hehellhe"
return (str.charAt(0)) + (str.charAt(1))+str+(str.charAt(0)) + (str.charAt(1));
why (str.charAt(0)) + (str.charAt(1)) is returning 205 instead of "he" and why the same statement is returning "he" at the end?
Expressions are evaluated Left-To-Right.
Hence first two chars are evaluated and added, which results in a char (value = 205).
Next this char(=205) is added to a string, which results in String.
Hence the strange output.
Fix:
Use a StringBuilder instead
public static void main(String[] args) {
String str = "hell";
StringBuilder buff = new StringBuilder();
buff.append(str.charAt(0))
.append(str.charAt(1))
.append(str)
.append(str.charAt(0))
.append(str.charAt(1));
System.out.println(buff.toString());// prints 'hehellhe'
}
This is happening because the values of the ASCII codes of the characters are being added instead of being concatenated. In ASCII, h is represented as 104, and e as 101. The concatenation isn't working, probably because both operands in this case are chars/integers.
Related
This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 4 years ago.
IntelliJ IDEA Capture
Why i am getting 152, I think it will give me an error.
Please explain it.
public class character {
public static void main(String[] args) {
char myCharValue1 = 'A';
char myCharValue2 = '2';
char myCharValue3 = '%';
System.out.println(myCharValue1 + myCharValue2 + myCharValue3);
}
}
That is because chars refer to a number, which in turn has an ASCII representation.
Looking at an ASCII table you can see that the chars A, 2 and % have following values respectivly: 65, 50 and 37.
Adding those numbers together, you'll end up with 152 which is what you got in your example.
To print out those chars you could use following:
System.out.printf("%s%s%s&n", myCharValue1 + myCharValue2 + myCharValue3);
Which will print A2% (and a newline)
The concatenation + is for String. What you're doing is adding the numeric values of your chars and printing them together.
If you start with "" and then use + as Patrick Parker shows in his comment, it will become concatenation instead of simple addition and you'll get the result you expect.
This question already has answers here:
How does the compareTo() method, compare strings? [closed]
(2 answers)
Closed 4 years ago.
Let's say i write this:
String a = "Hello";
String b = "Goodbye";
int compare = a.compareToIgnoreCase(b);
System.out.println(compare);
What will the printout be?
It compares character by character for each string.
For example in this case
When a = "hello" and b = "Goodbye"
It checks first character of a with first character of b and computes the relative difference, if it's same then it checks the next character, else it computes the difference and returns it.
If string a is greater than string b it returns a positive difference else it returns a negative difference.
in this case the out are be 1, because H comes after of G
This question already has answers here:
The concatenation of chars to form a string gives different results
(5 answers)
why does a char + another char = a weird number
(4 answers)
Closed 8 years ago.
We had an odd thing in our logging happen today. Printed is a list of integers and longs separated by commas, like the following code:
public class Main {
public static void main(String[] args) throws InterruptedException {
long l = 10;
System.out.println(l + ';' + "text");
}
}
The problem was that the ; disappeared from the output.
The problem here is caused by the overload of the + operator. It acts in one way when operating on a String and a long, and another when operating on a char and a long. When one of the operands is a string it will try to cast the other operand to a string if it's not already one and then concatenate the two.
But when the operators are numbers, like int and long, the + operator acts as the normal mathematical plus operator. And since char is a number and nothing else, ';' + l is treated as a numerical operation and thus the output of the code in the question is 69text and not 10;text.
This question already has answers here:
First char to upper case [duplicate]
(7 answers)
Closed 8 years ago.
I am trying to get the first character of a string to convert into an upper case. How is this possible?
For example;
String c = "hi steve, how are you doing?";
I want the 'h' of 'hi' capitalized. How do I do that using code?
Thank you in advanced!
c = Character.toUpperCase(c.charAt(0))+c.substring(1);
You can use String's charAt method to get the first character and the Character toUpperCase method to convert it to upper case. Then use the substring method to get everything after the first character.
String c = "hi steve, how are you doing?";
c = Character.toUpperCase(c.charAt(0)) + c.substring(1);
System.out.println(c);
Use of stringbuilder creates less strings:
StringBuilder s1 = new StringBuilder("hi steve");
s1.replace(0, s1.length(), s1.toString().toLowerCase());
s1.setCharAt(0, Character.toTitleCase(s1.charAt(0)));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java string replace and the NUL (NULL, ASCII 0) character?
I'm doing some String algorithms in Java, and i noticed that wherever i include a char with the value of 0 (zero) it marks the end of the String. Like this:
String aString = "I'm a String";
char[] aStringArray = aString.toCharArray();
aStringArray[1] = 0;
System.out.println(new String(aStringArray)); //It outputs "I"
What's the reason/cause of this behaviour?
The character '\0' is the null character. It's a control character, and it does not terminate the string, that's not how strings work in Java (that's how they work in C, though.)
For some additional insight, add the following to you code:
System.out.println(new String(aStringArray).length());
for (Byte b : new String(aStringArray).getBytes()) {
System.out.print("["+b+"]");
}
Your rendering system (console or output window) is not displaying everything.
I'm wondering if you have posted your actual code. When using the String(byte[]) constructor the actual length of the string is dependent on the contents of the array.