Reputation: 428
I am attempting to have a java program output every combination of two characters. I made code like this:
package foo;
public class CombineChars {
public static void main(String[] args) {
for(char a = '0'; a <= 'z'; a++){
for(char b = '0'; b <= 'z'; b++){
System.out.println(a+b);
}
}
}
}
What I expect is an output that looks like this:
00
01
02...
10
11
etc. But I get:
96 (First value)
97
98...
(Fluctuates here with going to like 220 then down to 150)
(Ends on) 244
Why does it do this and how can I fix it? Also I am open if there are better ways of achieving this.
Upvotes: 2
Views: 37
Reputation: 37645
It's converting the char
s to int
s and adding as numbers.
If you want to do String
concatenation you have to do
a + "" + b
instead.
Upvotes: 4
Reputation: 201439
You are performing integer math here,
System.out.println(a + b);
convert one (or both) arguments to a String
. Like,
System.out.println(String.valueOf(a) + b);
Upvotes: 3