Hello all welcome back. Let's discuss the solution for the program that I provided in the last lecture. I'm just going to bring up the eclipse. And I named the classes reverse characters. Let's maximize it. And let's get started.
The first thing we need here is the main method. So public static, wide and main extents This is just a normal convention, or you can say format for the program. So we need the main method in every program. Now the first thing I want to do is I want to provide the input right. So as I said, I will provide the input by taking it from the user. So I'm going to use this scanner class from Java.
So there is this import statement for scanner Java dot util dot scanner. And I'm giving it I'm going to give it a name called scan and then you can enter and then we will take the end input from system write the console. So we will just say system dot n, then let's print something because we need to tell the user to type it right to type the input. So I'm gonna say, Please enter string. So the user should enter a string after that. And let's capture the string into a variable.
So I'm gonna say string original. So this is our original string, and we capture the string using scan. This is the reference dot next line. So the next line is gonna get the line whatever user enters. After this, I want to make make one more verification. So I will do a while loop.
And I will say if original is empty, so I'm gonna say if it's empty or if original so Reasonable is equals to equals to null. So if the user either provides an empty string or a null string, then I'm going to tell the user that dude, this is something not accepted. So please enter a valid string. So I'm going to say please enter a valid string. And I can see empty and null strings, not accepted, right. I can just say this.
So why I have put that into the while loop. Because in the next line, after showing him the method, I'm going to again prompt him to enter the string. So again, in the next line, we will say scan dot next line, and we will keep that in original. So this way, we prompt the user until he enters a valid string. If we just hit the return key on the keyboard, every time he will get this prompt to enter a valid string. Once we get the string, we should close the scan.
So I'm just gonna say scan close up to this portion, what we have done is we have read the string, then I'm gonna write a method to actually reverse the string. So I'm gonna say, private string because it should return the string after reversing it. And reverse cars, maybe reverse reverse characters, because I think this is the same name of the class I want to do. I don't want to give the same name reverse characters. And it should take an input with the input is gonna be original thing we can we can give it any name. Actually, they are not related.
We can give it any name. It doesn't have to be same. So I'm just asking if the original string just to show you that it's different. Although we will be providing this to this method, when we're going to call it but these names doesn't have to be same Then it should return a string, right, which is going to be reversed. So let's define a string here, I'm gonna say worse, something, and let's initialize it to an empty string right now. And then let's return reverse.
Now it's not going to complain, because it is returning a string. So it was complaining that you have to return a string. Now let's write the logic to what I want to do here is I'm gonna loop over my original string. So for int i, and I want to start from the end, as I said, I want the string completely reversed. So I want to start from the end, I'm going to say, original string dot length minus one, because dot length gives you the complete string, but the index is one after the actual index, right? Because index starts from zero and length is going to give you a complete thing.
So Let's assume if your length is 10, and indexes, so and you have strings like zero to nine, the index is gonna be nine, right? It's not gonna be 10. So we start from length minus one. And then we come down to the initial the first characters, I'm going to say until it's greater than equals to zero. And then we reduce it right, i minus minus. And that's what I want to do, I want to start from the then in my reverse variable that I created this string.
In that I just want to capture it right. So I will say reverse equals to reverse plus original string. Now take a look. What we want to do is we want to just get the last character and put it here and then subsequently, we want to Add the second last character to this one, then the third last two, this one then has likewise right. So if we have hundred characters, we will start from the hundred. And we will come to 9998 9796.
Likewise up to the first character, and we will start adding to this variable, which will give us the completely opposite string, the reverse string. So I'm going to say, original string dot character at i. So this method gives us the character at a particular index. And we are looking for a particular index, it's gonna give us that once we do that, we actually just completed the method for reversing this thing. The only thing we want to do right now is just to call this method in the main method. And the warning you see should be it's never used locally right?
So we actually need to just call it and that be any we're gonna do. I'm gonna say great. An object right now. So let's just create an object. And I'm gonna say output equals to new. And let's just copy back.
And this guy, so we just created an object reference, then I'm gonna just create one more variable string, maybe reverse characters or something like that to as cast, maybe write something like that more descriptive. And then output dot, I'm just gonna call the method that we created. So reverse characters is the method that we created. In that we want to provide the original string. This is the original string that we captured from the user. Right.
And, finally, we just want to print it. Super simple. So I want to print the reverse characters. That's about it. So let's try to run In this code and see what we got, and I'm just gonna right click run as Java application. Now it prompted me to enter the string, right.
And that is what we want it to. So let's just take the same example it is a test string. And it gives me the complete opposite, COMPLETE REVERSE. Now, let me just try to run this code again. And I want to show you a few things that I did in this code, right. So it's asking me to enter a string, if I just hit enter right now, which means I'm just giving it an empty or null string.
So if I hit right now, it's going to give me the message, please enter a valid string, empty and null strings are not accepted. And I can again type something, but if I again, hit enter, it's again gonna give me the message. So is many times I'm just going to hit enter or provide the null or empty string. It's going to give me the error message. But after that, I can try to type in some new strain, maybe just like tests. to string and it's just gonna reverse everything that I provided.
So that's pretty much it, it completes the program. I will just say guys, as always practices The only thing you gotta do. Let me know when you have any questions and I'll see you in the next lectures. Thanks for attending this one.