[MUSIC] So why don't you implement a method that converts a string to a number? >> Okay, I want to go through some examples to make sure I understand the question correctly. Is the string going to be integer? Can it be floating point? >> Well, ideally, yes, but let's start by just thinking about how you would do an integer. >> Okay, so I can assume, let's say, if I'm given a string 123, and I should get back 123. Is that correct? >> Yep, mm-hm. >> Okay, and I don't have to consider numbers like e or pi, anything like that, right? >> No, no, and in fact, you can stick with positive numbers for now. >> Okay, and can I assume the input is just a string, plant string? >> Yes. >> Okay, so public int strToint and input is a string. Is a string. And can the string be empty? >> Sure, yeah, so think about what you might do. >> All right, should I be returning zero, or should I prompt the user that they should enter a different number? What should I do? >> Well, talk about why you would do each of those things. >> Okay, well, if it's required for the user to enter a number, I probably will prompt them the error. But if it doesn't really matter, I can just default to zero. I can come back to this case and add a comment here. Is that okay? >> Sure. >> Okay, so- >> Actually can I pause for a second and add a comment. >> So should I consider if the input string is empty? >> Yeah, definitely make sure that you handle that case. >> How should I handle, should I throw an exception to prompt the user that there has been an error in the input or should I just return zero? >> Which do you feel like is a better solution? >> Well, if it's absolutely crucial for the user to enter a number, then I'll probably throw exception, but if it doesn't really matter, I can just default the value to zero. >> Okay, if you did that, how do you think you'd distinguish between a zero value and a zero? >> That's very true. If we're assuming that all of the inputs are non-negative numbers, then we could return a negative number to indicate there has been an error too. >> Okay, good, good idea. >> Okay, so let me make sure that the string is not empty. And if that's the case, I will just return -1 for simplicity for now. And I think there are other corner cases, like maybe the string is a space or non-numerical values. Should I be considering that right now? >> Not right now, but we'll come to that. >> Okay, [CROSSTALK] okay, so I'm just going to add a comment here that says non-numerical cases. Edge cases. All right, so I think what I'm going to do is basically converting. I think there's a method call that just converts the string to a number. Is it okay if I use that library function? >> No, can you do it without the library function, please? >> Okay, so I think I'll try to get somehow get each individual digit. I think there's a library function that converts the string to a char array. Is it okay if I use that? >> Sure. >> Okay, so I declare a char array [SOUND] I think. Is it okay if I don't have the exact syntax for? >> Yeah, that's fine. >> Yeah, it's hard to remember the API without the code doc or the Javadoc. Okay, so input, okay. So now I have the string parsed into a char array, I can loop through the array, so. And less than, and I know it should be length not, because length is a field with arrays, not a method call. And I'm going to increment my index, and here we are. And so if I'm looping through each digit, I'm going to multiply it by powers of 10s. So I'm going to need an integer that keeps track of the value that I want to return in the end. And so I should add the digits to this value and multiply it by powers of 10, and there is a pow function in the math library. So (10, i), and then I can just return value in the end. >> All right, you said you wanted to add that value to value every time. You probably meant- >> Yes, sorry, I missed the plus sign. There you go, yeah. >> Great, so let me ask you to think about the body of your for loop. >> Okay. >> There's a slight problem with it. Can you figure out what's wrong? >> Yeah, okay, let me go back to the example that I had and then see. So I'm going to have char array would be equal to after, it would be equal to one, two, and three after I parse the string. And wait. I'm looping through the front to the back, which means my number would be reversed when I return this value. So what I should be doing is have this equal to charArr.length. So then I would just loop backwards, so then the values are oop, and then there's off by one, error right there [SOUND]. And then as long as i>=0 and then i--. So what this will do is you'll get the first or the last index first, and then multiply that to the nth when the number is correct. >> Great, okay, so that fixes one problem. And I still see one problem that might lead you to get, that will lead you to get the wrong answer here. >> Okay. Hm, I'm not quite sure what that error is? Could you possibly give me some hints? >> Sure, well, let's think about the char and think about how strings are represented. >> Okay. >> How characters are represented. >> Well, I know these are ASCII characters, and ASCIIs have their own values. [LAUGH] I forgot to convert these characters to actual integers. So again, I think there's a function call that just converts the char to a int. Is that okay if I use it? >> Sure, go ahead and use that for now. >> Okay, string, or I think it's actually in the character class. [SOUND] cter.toInt, toDigit maybe? Yeah, and I think this should fix the problem. >> All right, great, so there is a library call. And I am confident you could look it up if you needed it, but how might you do that? Can you think of a way, knowing about ASCII, how ASCII works, can you think of a way that you could solve that problem converting the char, the char, to its int value without using that library call? >> So I know they're ASCII values, and they have unique ASCII values, and it starts with zero and then goes to nine. I guess they're consecutive, so if I subtract the value from zero, then I would get the actual numeric value since the difference is same as the numeric value. >> All right, good, that's the right idea. >> All right, would you like me to implement that? >> No, that's fine. >> Okay. >> All right, well, thank you very much. >> All right, thank you. >> All right, great. I see a little problem with the body of your for loop. Could you maybe just verify, check that that's correct? >> Okay, so let me go back to my example. So if my input is 123 as a string, then my char array would look like this, 1, 2, so it will be parsed into different digits. And then I accumulate that in value, so value is started with zero. And then I look at index 0, which is 1, and then I do 1 times 10 to the 0th power, which is 1, so then value becomes 1. And then I go to the second digit which is, so let me write in i=1, then I'm on the second index, which gives me 2, and then 2 times power of 10 to the 1th, which gives me 20. Wait, then I'm going to have the number reversed. So, let me see. I would have to do some math to figure out what the i value would be. >> So I'll just suggest maybe the Math.pow Is kind of throwing you off a little bit. Try a solution that doesn't use Math.pow. >> Okay, well, basically what I want to happen is to have 100 plus 20 plus 3 to give me 123. So let's see. I guess what I can do is, instead of doing this. So again, if I'm looping from the beginning to the end, then, I would loop through 1 first. Then I loop through the loop three times. So if I multiply 10 each time, I would get 100. And then for two I'm going to have two iteration left. Yeah, and then I'll be, when I have three iterations, I only multiply it two times and then this one I multiply it by one times. So I think that should work, so let me just write that out. Yeah, so I would use the current value multiplied by 10. Actually, let me take what I said back. When I have three iterations, I will still be multiplying ten by three times, but because value is initially zero, so it's going to stay zero so it doesn't really matter. And then I can just add the character that I'm looking for here. Yeah, so I think this will work, yeah. >> All right, great. So you've got the right logic now, but there's still a small problem. Do you see it? >> No, not really. >> So, think about the way that chars and strings are represented, as opposed to the way numbers are represented. >> Okay, well I know these are ASCII characters, and they're represented by ASCII values. I guess ASCII values are different from integers, and I'm adding the character right away. So if I remember correctly, there's a method in the character class that converts it to a digit. Is it okay if I use the library function? >> Sure, it's okay. >> Okay, so if I do this, Character. Yeah, the value that gets returned in this case would be much bigger than what I intended it to be. So this should fix that problem. Let me just run through this example again, to make sure that it absolutely works. So again, I have the char array, and when i = 0, I have value = 0 * 10 + 1. So then I have 1, and when i = 1, I have value which is 1 * 10 + 2 and then that gives me 12. And when i = 2, I have this times 10 plus 3 which gives me 123. So this should work. >> Okay, great. >> Awesome, thank you. >> Thanks.