If you’re new to programming but want a career in tech, I HIGHLY RECOMMEND applying to one of Springboard’s online coding bootcamps (use code ALEXLEE for $1,000 off): bit.ly/3HX970h
Really enjoyed working through this practice program ... will be working through the rest of your practice programs and tutorials! Please continue uploading programs and tutorials - they are invaluable. As always, thanks loads Alex!
Here's by far the simplest solution I've found so far (with user input): Scanner scan = new Scanner (System.in); System.out.println("Enter a String: "); String s = scan.nextLine(); System.out.println(new StringBuilder(s).reverse().toString());
@@keithtammi2106 you would still need to create a reverse method. private static String reverse(String nextLine) { StringBuilder stringBuilder = new StringBuilder(nextLine); return stringBuilder.reverse().toString(); } Then that would work.
That was a great tutorial. I didn't expect that you'd explain how would the computer read the code. That made me easier to compose in my mind all the codes that you've written. Thank you so much.
A more efficient way of coding this is to just make it one for loop to decrement through the initial String but making a int tempIndex starting at 0, counting through the decrements, to add each char to the array using the string constructor to pass the now reversed char array to a string.
I found an easier way! public String reverseString(String s) { String reversedString = ""; for (int i = s.length()-1; i >= 0; i--) reversedString += s.charAt(i); s = reversedString; return s; } Basically, you take a string as a parameter. Then you create an empty string. We'll need it later. Then, you start a loop which basically says to copy each character of "s" starting from the left to this empty string. then set "s" to the reversedString and return.
I think this code is a lot easier, just so we don't have to use arrays: public class ReverseString { public static void main(String[] args) { String test = "dog"; // have the string "dog" reverse(test); // reverses and prints out dog } public static void reverse(String s){ StringBuilder result = new StringBuilder(); // creating a new string called result for (int h = s.length() - 1; h >= 0; h--) { // go through the s backwards result.append(s.charAt(h)); // appends or add the characters into our new string result } System.out.println(result); // print out the reverse version } }
or you could simply have something like this: public class ReverseString { public static void main(String[] args) { String test = "dog"; // have the string "dog" reverse(test); // reverses and prints out dog } public static void reverse(String s){ for(int i = s.length() - 1 ; i >= 0 ; i--) { System.out.print(s.charAt(i)); } } }
Hi Alex , Instead of the for loop you wrote at 6:42, we can reach the result by creating a string as follows. I wanted to write to support. Which one is better for performance, I don't know :) String reversedString = new String(letters); return reversedString;
he couldve used reversedString += charAt(i) while decrementing the i or even used a StringBuilder, thats beyond the point, its only to teach you the logic, did i mention its free?
Thank you very much for this video! I am studying applied informatics and I’m in the first semester. During corona everything‘s a little complicated, so I’m even more glad that you uploaded this tutorial. Have a good one! ☺️
A few questions: 1. line 11 - Is char array an object, since you use "New"? 2. line 21 - Does the + operator add ANY datatype to a string, since letters[i] is a char? Great videos, btw :)
Hey Alex I have recently joined your discord server and wondered if u are available anytime as I have some questions as I have just started coding. Love your content by the way! Much easier to learn Java with you than any channel I have found before.
Love your tutorials man. would this shorter bit of code be okay as well? achieves same result: String string = "Hello"; char buf; String reversed = ""; int i = string.length() - 1; while (i >= 0) { buf = string.charAt(i); reversed += String.valueOf(buf); i--; }
Hi, is it possible for you to organize java tutorials and java OOP tutorials separately. So that , it would be easier to find. Thanks. You are doing great!
Which code is more efficient, the one in video or the one below? String text = sc.nextLine(); int[] chars = text.chars().toArray().clone(); for(int i=chars.length-1; i>=0; i--) { char c = (char)chars[i]; result += c; }
Great video. It really makes programming a lot more fun when doing programming exercises. Btw, what's wrong with the for (int i = s.length() - 1; i >= 0; i--){ System.out.println(s.charAt(i)); } ? I mean, the code worked fine and the result is good too. Why did you have to create another for-loop?
Hey, there's another, much simpler way, by using String Buffer class. The beer's on you, guys. String One = "JavaTutorial"; StringBuffer sbuff = new StringBuffer(One); System.out.println(sbuff.reverse());
Here's one of two ways I do reverse String: public static void reverseString() { String dog = "dog", dogs = ""; int reverse = dog.length(); for(int i = reverse - 1; i >= 0; i --) { dogs = dogs + dog.charAt(i); } System.out.println(dogs); }
with the variable letterIndex you have some sort of counter for the reversed string/the char array. you start at index 0 and go up thus filling the array slots bit by bit with the characters of the string you want to reverse. if you used 0 or any constant number instead, you would redifine the character at the same slot with each itteration instead of moving to the next slot. (it's been 1 year but i still hope this was helpfull haha)
hi , thank you for the explaing , but can you plz explain how to create A program in which all arguments passed to the input string are displayed in reverse order. For example, if 2 arguments were passed - make install, then llatsni ekam should be displayed. Note *: to parse a word by letter, you must use the charAt () function. For example, str.charAt (i) will return the character at position i in the word written to the string variable str. Str command
Love your channel/videos.... Was hoping to get your advice on something... I recently finished a course on java that was 70+ hours worth of video..... So I know the basics of the language.. Wondering where i should go from there... For example... Currently i have no idea where to start with building a project, for example, say a game of tetris, i have no idea where to start on this. Any advice? Thanks!!
This code is cleaner and it does the same thing: class HelloWorld { public static void main(String[] args) { System.out.println(reverse("Alex")); } static String reversed = ""; public static String reverse(String s){ for(int i = s.length() - 1; i >= 0; i--){ reversed += s.charAt(i); } return reversed; } }
hey @Alex Lee, thanks for the help, I`ve been looking all around, and you are the best at it. Short and to the point. Anyway, I´m using Netbeans, not Eclipse. cause of school. so for some reason, its not working. It aint broken, it´s just not printing what it is supposed to. public static void main(String[] args) { String r = reverse("dog"); System.out.println(r); } public static String reverse(String s) { int i; = 0 ; i--){ letterIndex = s.charAt(i); letterIndex++; } String reverse = ""; return reverse; } so if you , or anyone in the comments could help out I would really appreciate it. Thanks, *firm handshake from Argentina
Alex Lee ohhhhh ok I see but you wouldn’t be able to read it using the ArraystoString method? Thanks for the feedback btw not hating or anything just want to see all the possible ways it could be done, I love coding too! 😊
Great tuts as always....you could simplify this one though and not even use a char array. Just concatenate the charAt(i) straight into your empty string and return the result. :) public static String reverse(String s){ String result = ""; for (int i = s.length() -1; i>=0; i--){ result += s.charAt(i); } return result; }
1. Create a program that will let the user to enter decimal numbers and will sort the inputted values into descending order. Make sure that the user will be able to decide how many numbers he or she wanted to enter. Use one dimensional array. 2. Create a program that will let the user to enter decimal numbers and will sort the inputted values into ascending order. Make sure that the user will be able to decide how many numbers he or she wanted to enter. Use one dimensional array. 3. Using the following initial values, 90,87,1,99,34,23,1000 and 78, create a program that will search where an element is located on an index of the array. 4. Create a program that will let the user to enter decimal numbers and will search and display the location of an element. hello I'm a student also a beginner and I don't know how to solve this problems. thank you😇
this method is very easy. look. public class ReverseString { public static void main(String[] args) { String str= "Hello World!"; String reverse= new StringBuilder(str).reverse().toString(); System.out.println(reverse); } }
If you’re new to programming but want a career in tech, I HIGHLY RECOMMEND applying to one of Springboard’s online coding bootcamps (use code ALEXLEE for $1,000 off): bit.ly/3HX970h
Really enjoyed working through this practice program ... will be working through the rest of your practice programs and tutorials! Please continue uploading programs and tutorials - they are invaluable. As always, thanks loads Alex!
Here's by far the simplest solution I've found so far (with user input):
Scanner scan = new Scanner (System.in);
System.out.println("Enter a String: ");
String s = scan.nextLine();
System.out.println(new StringBuilder(s).reverse().toString());
new StringBuilder(s).reverse() is enough
toString() no need
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a Word: ");
String r = reverse(scanner.nextLine());
System.out.println(r);
@@keithtammi2106 you would still need to create a reverse method.
private static String reverse(String nextLine) {
StringBuilder stringBuilder = new StringBuilder(nextLine);
return stringBuilder.reverse().toString();
}
Then that would work.
This channel is keeping me sane as I study for my Java Final next Monday!! Super underrated channel, cant wait for the blow-up!!
Jorge Rivera thanks! Good luck!
I agree, Alex does a very very good job with explaining especially for beginners like me. I'm glad to find this channel.
That was a great tutorial. I didn't expect that you'd explain how would the computer read the code. That made me easier to compose in my mind all the codes that you've written. Thank you so much.
A more efficient way of coding this is to just make it one for loop to decrement through the initial String but making a int tempIndex starting at 0, counting through the decrements, to add each char to the array using the string constructor to pass the now reversed char array to a string.
Alex you know, I am beginner of java but after listen your video my enthusiasm is build up. I am really apricated by heart to see your amazing job
I found an easier way!
public String reverseString(String s) {
String reversedString = "";
for (int i = s.length()-1; i >= 0; i--)
reversedString += s.charAt(i);
s = reversedString;
return s;
}
Basically, you take a string as a parameter.
Then you create an empty string. We'll need it later.
Then, you start a loop which basically says to copy each character of "s" starting from the left to this empty string.
then set "s" to the reversedString and return.
Amazing and crystal clear explanation. Really enjoyed this practice program. Thank you Alex
I gotta admit, learning with this channel is much more fun, keep it up :D
I think this code is a lot easier, just so we don't have to use arrays:
public class ReverseString {
public static void main(String[] args) {
String test = "dog"; // have the string "dog"
reverse(test); // reverses and prints out dog
}
public static void reverse(String s){
StringBuilder result = new StringBuilder(); // creating a new string called result
for (int h = s.length() - 1; h >= 0; h--) { // go through the s backwards
result.append(s.charAt(h)); // appends or add the characters into our new string result
}
System.out.println(result); // print out the reverse version
}
}
or you could simply have something like this:
public class ReverseString {
public static void main(String[] args) {
String test = "dog"; // have the string "dog"
reverse(test); // reverses and prints out dog
}
public static void reverse(String s){
for(int i = s.length() - 1 ; i >= 0 ; i--) {
System.out.print(s.charAt(i));
}
}
}
that makes sense
the best java channel of all youtube.
Your videos are singlehandedly allowing me to pass my class
i think u are an angel. u are amazing. i learn how helpful humans are from ur kind work.
You are great Alex, Really njoyed working with you... I can easily understand all your concepts ... It's supper fun Thank you ❤️
Used this week 2 day 2 of Revature Java training. Very helpful. Thanks!
You got a new subscriber from India
omg i was finally able to run my first program after watching this video! this helped me so much!
Hi Alex ,
Instead of the for loop you wrote at 6:42, we can reach the result by creating a string as follows. I wanted to write to support.
Which one is better for performance, I don't know :)
String reversedString = new String(letters);
return reversedString;
he couldve used reversedString += charAt(i) while decrementing the i or even used a StringBuilder, thats beyond the point, its only to teach you the logic, did i mention its free?
I like when you explain everything again in the end. Thanks.
Thank you very much for this video! I am studying applied informatics and I’m in the first semester. During corona everything‘s a little complicated, so I’m even more glad that you uploaded this tutorial. Have a good one! ☺️
Excellent video, can't wait to see more!!
made this code to have ask for input --- works charm
A few questions:
1. line 11 - Is char array an object, since you use "New"?
2. line 21 - Does the + operator add ANY datatype to a string, since letters[i] is a char?
Great videos, btw :)
+ is used to concate string here letters
Much appreciated!
/First year CS student!
Hey Alex I have recently joined your discord server and wondered if u are available anytime as I have some questions as I have just started coding. Love your content by the way! Much easier to learn Java with you than any channel I have found before.
Crazywarrior123 sure! I’ll hop over in the discord and I’ll see what I can help you with
Pure gem this channel
I think Alex can be patented as an cartoon character, super viewer friendly
public static void main(String[] arg){
String myStr = "hello";
String h ="";
for(int i = myStr.length() - 1; i >= 0; i--){
h = h + myStr.charAt(i);
}
System.out.println(h);
}
Wouldnt this be easier?
Exactly
Alex you are good teacher
String name = "Shirin";
for (int i =name.length()-1;i>=0;i--) {
System.out.print(name.charAt(i));
}
I Thought in starting you are going to remove ln form S.out.println but after that in last you are explaining it amazingly
thank you sm Alex, i'm a beginner and your videos are really helping me to understand things.
{
String s="Dog",rev="";
Int l=s.length();
For(i=l-1;1>=0;1--)
{
rev=rev+s.charAt(i);
}
sysout(rev)
}
if java can make dog to god, imagine what java could do to you
Lol
lmao
Could also turn god into dog
uoy
@@ramosojustinken9894 😂😂😂
This guy got my intrest in computer science
thanks for the help man, keep up the great work!
Thank you so much
Woooow, dude this tutorial is great and very helpful! Thanks
INSANE INFORMATIONS!!
Very Good i had some problems on learning how to use Eclipse but very good video. I need examples.
Very Clear , Thank you Alex
Really sad that you decided to stop making Java tutorials. I believe you have a gift for teaching! Best wishes, man!
@Dragomir Petrov, another Bulgarian here who agrees with you!
Love your tutorials man.
would this shorter bit of code be okay as well? achieves same result:
String string = "Hello";
char buf;
String reversed = "";
int i = string.length() - 1;
while (i >= 0)
{
buf = string.charAt(i);
reversed += String.valueOf(buf);
i--;
}
This is like the most complicated version of explanation for an easy problem.
best explanation on whole youtube
thank the lord I found these videos. I'm currently taking a Data Structures and Algorithms course and I am L O S T.
You made it more complicated than it needed to be 🙂
very well explained alex :)
Was also hoping you could do another video on this, exactly the same but with an enhanced for loop.
Hi, is it possible for you to organize java tutorials and java OOP tutorials separately. So that , it would be easier to find. Thanks. You are doing great!
Thank you and ily you saved my life !!
Why is it s.length() - 1? What does the minis one do?
Which code is more efficient, the one in video or the one below?
String text = sc.nextLine();
int[] chars = text.chars().toArray().clone();
for(int i=chars.length-1; i>=0; i--) {
char c = (char)chars[i];
result += c;
}
Hey man you have helped me a lot ! Bless ya
Great video. It really makes programming a lot more fun when doing programming exercises. Btw, what's wrong with the
for (int i = s.length() - 1; i >= 0; i--){
System.out.println(s.charAt(i));
} ? I mean, the code worked fine and the result is good too.
Why did you have to create another for-loop?
just to show what else you can do
Nice Alex. great explaination
Why do you need an other for loop if this one reverses the string?
for(int i = str.length() -1 ; i > =0 ; i--) {
System.out.print( str.charAt(i));
}
Yes but it stores it in a array of char, the other loop helps in putting that elements in a string.
i did this and worked, is it a must to do your way?
public static void main(String[] args){
String rev = "hello";
for(int i = rev.length() -1; i >= 0; i--)
System.out.println(rev.charAt(i));
}
Thank you very much! this is super helpful ❤️
Hey, there's another, much simpler way, by using String Buffer class. The beer's on you, guys.
String One = "JavaTutorial";
StringBuffer sbuff = new StringBuffer(One);
System.out.println(sbuff.reverse());
Awesome!!! It was so much simpler and it's easier to remember.
This is briefly how I did it:
for (int i = len -1; i > 0; i--) {
rev += norm.charAt(i);
}
are u gonna do API like Lamda and Stremes ,btw grreat vids
Love your video, Thanks
It works just fine 💎👌
Here's one of two ways I do reverse String:
public static void reverseString() {
String dog = "dog", dogs = "";
int reverse = dog.length();
for(int i = reverse - 1; i >= 0; i --) {
dogs = dogs + dog.charAt(i);
}
System.out.println(dogs);
}
Thanks alex!
Hello Alex. Line 15 at 9:26 would it be correct to have something like letters[0] instead of creating the letterIndex variable in line 13?
with the variable letterIndex you have some sort of counter for the reversed string/the char array. you start at index 0 and go up thus filling the array slots bit by bit with the characters of the string you want to reverse. if you used 0 or any constant number instead, you would redifine the character at the same slot with each itteration instead of moving to the next slot. (it's been 1 year but i still hope this was helpfull haha)
hi , thank you for the explaing , but can you plz explain how to create A program in which all arguments passed to the input string are displayed in reverse order. For example, if 2 arguments were passed - make install, then llatsni ekam should be displayed. Note *: to parse a word by letter, you must use the charAt () function. For example, str.charAt (i) will return the character at position i in the word written to the string variable str. Str command
can u please make a video on the USSDMenu program
public static String reverseStr(String str) {
String finalStr = "";
for(int i=str.length()-1; i >= 0;i--) {
finalStr += Character.toString(str.charAt(i));
}
return finalStr;
}
public static void main(String[] args){
String rev = "hello";
for(int i = rev.length() -1; i >= 0; i--)
System.out.println(rev.charAt(i));
}
thanks bro 👍
great program.
Thank you!
I like your tutorials, keep it up :)
thanks!
Love your channel/videos.... Was hoping to get your advice on something... I recently finished a course on java that was 70+ hours worth of video..... So I know the basics of the language.. Wondering where i should go from there... For example... Currently i have no idea where to start with building a project, for example, say a game of tetris, i have no idea where to start on this. Any advice? Thanks!!
Good for interviews!!!
This code is cleaner and it does the same thing:
class HelloWorld {
public static void main(String[] args) {
System.out.println(reverse("Alex"));
}
static String reversed = "";
public static String reverse(String s){
for(int i = s.length() - 1; i >= 0; i--){
reversed += s.charAt(i);
}
return reversed;
}
}
you are awesome buddy
hey @Alex Lee, thanks for the help, I`ve been looking all around, and you are the best at it. Short and to the point. Anyway, I´m using Netbeans, not Eclipse. cause of school. so for some reason, its not working. It aint broken, it´s just not printing what it is supposed to.
public static void main(String[] args) {
String r = reverse("dog");
System.out.println(r);
}
public static String reverse(String s) {
int i; = 0 ; i--){
letterIndex = s.charAt(i);
letterIndex++;
}
String reverse = "";
return reverse;
}
so if you , or anyone in the comments could help out I would really appreciate it.
Thanks, *firm handshake from Argentina
Could’ve just skipped the extra step of making a string and returned the array too right?
Estevan Prado you could return the array but to print out an array you have to loop through it to get each element
Alex Lee ohhhhh ok I see but you wouldn’t be able to read it using the ArraystoString method? Thanks for the feedback btw not hating or anything just want to see all the possible ways it could be done, I love coding too! 😊
Estevan Prado haha I love coding too :) and yes you’re right, there are methods that’ll convert arrays to strings for ya that would do the same thing
Every time we concating a new character to the string are we creating new string object?
i still dont understand wht is the String reverse = ""; for
Initializing an empty string
@@ETSIRAVE in order to concatenate the characters of given string in reverse order..
how to use input hello world and output world hello in java
you can basiclly write print instead of println?
Nice custom keeb, silent tacts?
Great tuts as always....you could simplify this one though and not even use a char array.
Just concatenate the charAt(i) straight into your empty string and return the result. :)
public static String reverse(String s){
String result = "";
for (int i = s.length() -1; i>=0; i--){
result += s.charAt(i);
}
return result;
}
Is believe he's aware this but doesn't want to do anything advanced for this tutorial, for the sake of beginners
thanks man
Why there is a second for each loop can u tell in first only we are taking the character in reverse order can not we directly print?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text;
String tmp = "";
text = sc.nextLine();
for(int i=text.length()-1;i>=0;i--){
tmp+=text.charAt(i);
}
System.out.println("Result : "+tmp);
}
is it with return and with parameter method?
Just a modified way:
public static String reverseString(String inpStr) {
char[] reverChar = new char[inpStr.length()];
for(int chrIndx=0, i = inpStr.length()-1; i>=0; i--, chrIndx++) {
reverChar[chrIndx] = inpStr.charAt(i);
}
return String.valueOf(reverChar);
}
I like this java practice. I understand the concept a lot better. Thank you.
where is that keyboard from??
How can I reverse a string that the user inputs?
1. Create a program that will let the user to enter decimal numbers and will sort the inputted values into descending order. Make sure that the user will be able to decide how many numbers he or she wanted to enter. Use one dimensional array.
2. Create a program that will let the user to enter decimal numbers and will sort the inputted values into ascending order. Make sure that the user will be able to decide how many numbers he or she wanted to enter. Use one dimensional array.
3. Using the following initial values, 90,87,1,99,34,23,1000 and 78, create a program that will search where an element is located on an index of the array.
4. Create a program that will let the user to enter decimal numbers and will search and display the location of an element.
hello I'm a student also a beginner and I don't know how to solve this problems. thank you😇
this method is very easy. look.
public class ReverseString {
public static void main(String[] args) {
String str= "Hello World!";
String reverse= new StringBuilder(str).reverse().toString();
System.out.println(reverse);
}
}
This actually helped me more than the video - much more succinct and worked perfectly - thanks!
@@latedeveloper7836 you welcome dr. 🙋
Wow. Both thumbs up!
for(int i =str.length()-1;i>=0;i--)System.out.print(str.charAt(i));
I wish I was as skillful as you are
you are AWESOME ::)