Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Bryan Beasman
3,435 PointsWhy is my method returning numbers?
Please help!
I am attempting to solve this code challenge:
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
Here is the method I built:
public static String altPairs(String str) {
char[] strArr = str.toCharArray();
String newStr = "";
for (int i = 0; i < strArr.length; i += 4) {
for (int j = i + 1; j == i + 1 && j < strArr.length; j++) {
newStr += strArr[i] + strArr[j];
}
}
return newStr;
}
For some reason, the method is returning a string of numbers. For example: altPairs("firetruck") returned 207230
Why is this???
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsChange the code from:
newStr += strArr[i] + strArr[j];
To
newStr += strArr[i];
newStr += strArr[j];
should work. For a detailed explanation see here:
Haven't finished reading yet. But in general, be careful adding two char-s...
Bryan Beasman
3,435 PointsWelp, that solved my issue. But that is so weird. So it thought I was trying to add the two indices together and then cast the result to a String?
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsThe problem is in 'Arithmetic promotion' and in the ways += operator works as you can read from Stack Overflow post, or here:
http://www.codemiles.com/java/arithmetic-promotion-t3487.html
First of all, when you try to add two char-s you will get a number.
Why?
Because quote:
Under the hood a char is represented by a numeric value. The characters for the numbers 0-9 are stored in ascending numeric values. If we use 0 as a base value then simple subtraction can convert a 0-9 char into the equivalent int value
Why under the hood char is represented by number, I don't know for sure yet, but it is a fact...
All I can say that there is no straightforward way to convert two char to the third one... Because char represents one character. And you cannot add 'a' to 'b' and get 'ab', because 'ab' is a String...
So writing
System.out.println('a' + 'b');
Will give "195".
So coming back to your code:
When you write
newStr += strArr[i] + strArr[j];
First of all expresion inside is evaluated, and that is strArr[i] + strArr[j] and as you saw that gives a number.
And because that expression is evaluated first you got the number...
My solution avoids this problem...
There is also other solution:
newStr = newStr + strArr[i] + strArr[j];
It will also work.
But the operation like here:
newStr = newStr + (strArr[i] + strArr[j]);
Will NOT work.
As you can see: it is just simple Math, and you have to make sure do not add two chars first.
Bryan Beasman
3,435 PointsBryan Beasman
3,435 PointsMy code pasted a little weird. My apologies for the mess.
Moderator edit: Added markdown to code posted for readability.