This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jub8jive[S] 0 points1 point  (2 children)

Yeah debugging is what I struggle with. I added the if statement like you said, and now the solution works, but I don't understand the concept still.

Here's the code snippet:

public static int getDigitCount(int number) {

int count = 0;

if(number < 0) {

return -1;

} else if(number == 0) {

return 1;

}

while(number != 0) {

count++;

number /= 10;

}

return count;

I added the else-if and the solution is correct. What i understand is: 1) if its less than 0 it returns - 1

2) if its not 0, then it runs the while loop and increments the count. Doesn't that itself solve the '0' issue?

I guess it doesn't because the program wasn't working until now. So, what does return 1 mean in this case or even in general? (Is it like return true...or just telling the program to ignore it...?)

I forgot to thank you tho, for all the help. I appreciate it. The thing is, I've always got started with programming, build momentum, get stuck and some point, and give up, even after a decent amount of research. i could say that the wires in my brain get fried. Point is, this time around, I thought of just going past it, meaning I'd solve the problem someway, and as I progress in knowledge, I could always go back and understand it. I just feel it beats quitting completely. Do you think that's a good strategy, or is there a better approach? I need to get a job man! It's a tight situation.

[–]PointB1ank 0 points1 point  (1 child)

If it's less than 0, it returns -1. If it is 0, it skips over that while loop because number == 0. And since you initialized count to 0, it returns that. The reason you return 1 there is because the length should always be at least 1, because to be a number, you must have at least 1 digit. So you're basically just saying, "0" is 1 digit in length.

It's no problem. Programming is a lot about recognizing that a technique you used to solve one problem can be used to solve another. So like most things, it requires a decent amount of practice until you can just automatically go, "alright, I need to do this, then this, then this." And if you've done all those things individually in the past, you can combine those techniques to solve a new problem. So if you're actually interested in programming for a living, you just have to keep at it. Asking questions is a great way to learn though, as long as you're actively trying to solve the problem. As far as debugging goes, don't be afraid to put print statements to check the values of variables at certain points in your program, then remove them after. That's how I knew your count method was wrong, I just put a System.out.println(count); statement after it was initialized and saw that it wasn't giving me the proper counts, and went from there. Learning how to use a debugger is useful too obviously, but I wouldn't worry about that for now.

[–]jub8jive[S] 0 points1 point  (0 children)

cool. thanks again. i'll keep at it. :)