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
@jorgerivera70085 жыл бұрын
This channel is keeping me sane as I study for my Java Final next Monday!! Super underrated channel, cant wait for the blow-up!!
@alexlorenlee5 жыл бұрын
Jorge Rivera thanks! Good luck!
@remax1104 жыл бұрын
I agree, Alex does a very very good job with explaining especially for beginners like me. I'm glad to find this channel.
@werren8944 жыл бұрын
if java can make dog to god, imagine what java could do to you
@adarshmenon43564 жыл бұрын
Lol
@og_gaming23233 жыл бұрын
lmao
@zeeu3 жыл бұрын
Could also turn god into dog
@ramosojustinken98942 жыл бұрын
uoy
@satyasrilekhagandreddy7766 Жыл бұрын
@@ramosojustinken9894 😂😂😂
@vishy4 жыл бұрын
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!
@qwerasdfhjkio4 жыл бұрын
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.
@crazywarrior12325 жыл бұрын
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.
@alexlorenlee5 жыл бұрын
Crazywarrior123 sure! I’ll hop over in the discord and I’ll see what I can help you with
@marko86403 жыл бұрын
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());
@subhakar90153 жыл бұрын
new StringBuilder(s).reverse() is enough toString() no need
@keithtammi21062 жыл бұрын
Scanner scanner = new Scanner(System.in); System.out.print("Enter a Word: "); String r = reverse(scanner.nextLine()); System.out.println(r);
@DriveandThrive2 жыл бұрын
@@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.
@dozui3 жыл бұрын
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("Enter a String: "); String orig = s.nextLine(); s.close(); System.out.println("You entered : " + orig); System.out.println("Reversed to : " + reverseString(orig)); } static String reverseString(String input){ String result = ""; for(int i=input.length()-1; i>-1; i--) result = result + input.charAt(i); return result; } }
@searemebrahtu81132 жыл бұрын
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?
@sareer15 күн бұрын
Exactly
@kashivitagors7823 Жыл бұрын
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); }
@reshmashaik30914 жыл бұрын
You got a new subscriber from India
@ItzKingzz4 жыл бұрын
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 } }
@ItzKingzz4 жыл бұрын
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)); } } }
@hydrogennetwork8 ай бұрын
that makes sense
@linusziegler24133 жыл бұрын
I gotta admit, learning with this channel is much more fun, keep it up :D
@ClarkieReidri4 жыл бұрын
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.
@morgvng8437 Жыл бұрын
simple, you chose god and dog because in italian its translated with "dio cane" a very common phrase here
@thimiradk3 жыл бұрын
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); } }
@latedeveloper78363 жыл бұрын
This actually helped me more than the video - much more succinct and worked perfectly - thanks!
public static void main(String[] args){ String rev = "hello"; for(int i = rev.length() -1; i >= 0; i--) System.out.println(rev.charAt(i)); }
@randomguy30003 жыл бұрын
public static void main(String [] args){ Scanner scan = new Scanner(System.in); String word = scan.nextLine(); String reverseWord=" "; for(int i=word.length()-1; i>=0; i--){ reverseWord+=word.charAt(i); } system.out.println(reverseWord); } this is another easier way to do it
@marko86403 жыл бұрын
Thank's for the code!
@SS-ug1qy3 жыл бұрын
Why complicate when there is an easier way to reverse a string. String str = "Hello"; String reverse = ""; for(int i = str.length()-1; i>=0; i--) { reverse = reverse + str.charAt(i); } System.out.println(reverse); //if you want to check your string for palindrome you can add the bottom code. if(reverse.equalsIgnoreCase(str)) { System.out.println("word is a palindrome"); }else{ System.out.println("word is not a palindrome"); }
@marko86403 жыл бұрын
This is MASSIVELY helpful! Thank you!
@mamtasingha69494 жыл бұрын
You are great Alex, Really njoyed working with you... I can easily understand all your concepts ... It's supper fun Thank you ❤️
@TomaAndreiMusic Жыл бұрын
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; } }
@userx4814 жыл бұрын
Why not do this: String x = "Hare Krishna"; StringBuilder sb = new StringBuilder(); sb.append(x); System.out.println(sb.reverse()); Or this String input = "Gopinath"; char[] c = input.toCharArray(); for (int i = c.length-1; i>=0; i--) System.out.print(c[i]); And keep the brain fresh.
@codegirl20694 жыл бұрын
I've only thought of using a sb. Didn't know I could use .toCharArray Thanks :)
@sotebis26084 жыл бұрын
nice job!
@petsgarden29454 жыл бұрын
import java.util.*; public class Converter { public static void main(String[] args) { Scanner scan = new Scanner(System.in); scan.close(); String text = scan.nextLine(); StringBuffer buf = new StringBuffer(text); buf.reverse(); System.out.println(buf); } }
@cjjsggsjsh82064 жыл бұрын
you made that harder: package com.company; import java.util.Scanner; public class Game { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter The Word"); String word = input.next(); for (int i = (word.length()-1); i >=0; --i) System.out.print(word.charAt(i)); { } } }
@marko86403 жыл бұрын
Is there a way to reverse the whole sentence in that way? I'm new to Java, so everything I've tried so far failed miserably.
@rittenbrake16134 жыл бұрын
I think Alex can be patented as an cartoon character, super viewer friendly
@estevanprado20055 жыл бұрын
Could’ve just skipped the extra step of making a string and returned the array too right?
@alexlorenlee5 жыл бұрын
Estevan Prado you could return the array but to print out an array you have to loop through it to get each element
@estevanprado20055 жыл бұрын
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! 😊
@alexlorenlee5 жыл бұрын
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
@irvingpurata29394 жыл бұрын
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)); }
@cartoons__for__kids_Hindi4 жыл бұрын
Yes but it stores it in a array of char, the other loop helps in putting that elements in a string.
@kamalkaur14793 жыл бұрын
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
@conanimation3 жыл бұрын
are u gonna do API like Lamda and Stremes ,btw grreat vids
@marko86403 жыл бұрын
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());
@dafneylogan74332 жыл бұрын
Awesome!!! It was so much simpler and it's easier to remember.
@salaamdev3 жыл бұрын
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)); }
@ademgokce31364 жыл бұрын
String myString = "AdemGokceKahramanmaras"; String myReverseString = new StringBuffer(myString).reverse().toString(); System.out.println("Here is myString: " + myString); System.out.println("Here is myReverseString: " + myReverseString);
@user-sk4cw5lx4f3 жыл бұрын
Hello, I’m trying to reverse the order the lines from a text file. For example if the text file says “The cat in the hat Likes to eat Rats Because he’s so fat ” The output should be “Because he’s so fat Likes to eat Rats The cat in the hat “
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--; }
@LizzieSpot Жыл бұрын
Bro you made it a bit more hard because you can use print instead of println :- Code public class Main{ public static void main(String[] args){ String r = reverse("DOG"); } public static String reverse(String s){ char[] letters = new char[s.length()]; for(int i = s.length() - 1 ; i >= 0;i--){ System.out.print(s.charAt(i); // You should use print because it will print the whole // word in the single line/ } return s; } }
@shirinelmanson78393 жыл бұрын
Alex you are good teacher String name = "Shirin"; for (int i =name.length()-1;i>=0;i--) { System.out.print(name.charAt(i)); }
Why is it s.length() - 1? What does the minis one do?
@somkhit_phosavath Жыл бұрын
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); }
@pjguitar152 жыл бұрын
String newStr = ""; for (int i = str.length() - 1; i >= 0; i--) { newStr += str.charAt(i); } return newStr; this solution is much easier in my opinion
@AngieGiovanna8 ай бұрын
omg i was finally able to run my first program after watching this video! this helped me so much!
@dragomirpetrov51382 жыл бұрын
Really sad that you decided to stop making Java tutorials. I believe you have a gift for teaching! Best wishes, man!
@dimitarvelinov2772 жыл бұрын
@Dragomir Petrov, another Bulgarian here who agrees with you!
@justmrfresh3 жыл бұрын
for(int i =str.length()-1;i>=0;i--)System.out.print(str.charAt(i));
@techme10162 жыл бұрын
Could you please solve this. Input : my name is arbaz Output: arbaz is name my
@jenniferbijan19124 жыл бұрын
thank the lord I found these videos. I'm currently taking a Data Structures and Algorithms course and I am L O S T.
@anathadenver60272 жыл бұрын
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.
@Harshada16383 жыл бұрын
Thank you so much
@kvelez Жыл бұрын
great program.
@markk54954 жыл бұрын
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?
@adenosinetp103 жыл бұрын
just to show what else you can do
@eien72285 жыл бұрын
i still dont understand wht is the String reverse = ""; for
@f3-faithfitnessfinance4 жыл бұрын
Initializing an empty string
@f3-faithfitnessfinance4 жыл бұрын
@@ETSIRAVE in order to concatenate the characters of given string in reverse order..
@AktherBrothersOfficial Жыл бұрын
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; }
@CorporateGamer4 жыл бұрын
I ended up walking away as a better person. Thanks
@indranildutta42522 жыл бұрын
made this code to have ask for input --- works charm
@shaccc37282 жыл бұрын
This is briefly how I did it: for (int i = len -1; i > 0; i--) { rev += norm.charAt(i); }
@shykhsaif87812 жыл бұрын
You are a good programmer but not a good teacher Don't jump through the video but try to explain it .
@samarthvs33382 жыл бұрын
A less complex method- public class Main { static String reverse(String s) { String reversed = ""; for(int i = s.length()-1; i >= 0; i--){ reversed = reversed + s.charAt(i); } return reversed; } public static void main(String[] args) { System.out.println(reverse("0123456")); } }
@kakashi999082 жыл бұрын
yeah the char array was totally unnecessary and added more complexity.
@veervikramsingh8911 Жыл бұрын
sir How have you written String r = reverse("dog");
@keanaleong77453 жыл бұрын
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?
@krm.073 Жыл бұрын
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)
@MrEvol942 жыл бұрын
"Return" doesn't seem to exist in my java program 😅
@sheenvelayudhanrajeswari5794 жыл бұрын
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); }
@bennevisson7787 Жыл бұрын
can u please make a video on the USSDMenu program
@georgietrottier93124 жыл бұрын
I found a short and simple way Scanner n= new Scanner(System.in); System.out.print(“Enter Something: “); String name= n.nextLine(); char[] arr = new char[name.length()]; int k=0; for(int i=name.length() -1; i>=0; i-){ char last= name.charAt(i); arr (k)= last; k++; } System.out.print(arr);
@gnanechaithu97382 жыл бұрын
I think I am very late to discover this channel. Please cover dsa also.
@sheetalpatil85912 жыл бұрын
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?
@imtiazbasha33242 жыл бұрын
Amazing and crystal clear explanation. Really enjoyed this practice program. Thank you Alex
@hammershigh3 жыл бұрын
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 :)
@sahilparmar83822 жыл бұрын
+ is used to concate string here letters
@CrazyChaosClara3 жыл бұрын
Might be a stupid question... but why can't I create another string and call the method by writing s.reverse?
@marko86403 жыл бұрын
It's a totally valid question because I tried to do something similar in my code. It didn't work, so here I am, looking here for help. It bothers me, why it can't be simpler, like writing just a line or two of code. It's just writing a sentence backward.
@kamalkaur14793 жыл бұрын
can you please also help me Convert roman number to integer number?
@richardsonmoraes28933 жыл бұрын
the best java channel of all youtube.
@rafaelnistor16524 жыл бұрын
StringBuffer sb = new StringBuffer("My String"); System.out.println(sb.reverse());
@marinastr34293 жыл бұрын
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! ☺️
@emrekorkmaz54692 жыл бұрын
you can basiclly write print instead of println?
@sajedamirza62523 жыл бұрын
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!
@mjbuenaventura81692 жыл бұрын
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😇
@shives1792 жыл бұрын
how to use input hello world and output world hello in java
@DMPDEV3 жыл бұрын
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
@TheBoDuddly Жыл бұрын
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; }
@sesebere Жыл бұрын
Is believe he's aware this but doesn't want to do anything advanced for this tutorial, for the sake of beginners
@eduartzina3 жыл бұрын
hello, i may need some help here with a code i got. I have 2 string arrays with names. from a method, I feed with 2 string Arrays and return strings with names, and by using "string.join" I have to print all names present in arrays . any idea how to solve this?
@udayendubose11663 жыл бұрын
This guy got my intrest in computer science
@flashinstincts50323 жыл бұрын
i think u are an angel. u are amazing. i learn how helpful humans are from ur kind work.
@grenouilleflamboyante9783 жыл бұрын
great video. I tried to do it with Stack: public static String ReverseWithStack( String s) { LinkedList letter = new LinkedList(); String reverse = ""; for (int i = 0 ; i < s.length() ; i++) { letter.addFirst(s.charAt(i)); } for (int i = 0 ; i < letter.size(); i++) { reverse = reverse + letter.get(i); } return reverse; } It works but having 2 for loops that look similar bothers me
@himusardar83293 жыл бұрын
I didn't like the dog & god example. But I understood the method. Thanks for that ❤️.
@DC-xj2fe2 жыл бұрын
Used this week 2 day 2 of Revature Java training. Very helpful. Thanks!
@fizzics55883 жыл бұрын
where is that keyboard from??
@monicapacheco9332 жыл бұрын
Love your video, Thanks
@MOLOTONO3 жыл бұрын
Good for interviews!!!
@parvezquazi64242 жыл бұрын
best explanation on whole youtube
@cabreraaubrey45693 жыл бұрын
is it with return and with parameter method?
@yashwanthpraveen67982 жыл бұрын
This is like the most complicated version of explanation for an easy problem.
@rohatergun Жыл бұрын
Nice code but overcomplicated
@trazzysingh4 жыл бұрын
Hey i run like this public static String reverse(String str){ String return_revers= ""; if (str.isEmpty()) { str = return_revers; } char[] revers = new char[str.length()]; for (int i = revers.length - 1; i >= 0; i--) { return_revers +=str.charAt(i); } return return_revers; }
@oliverdivine52464 жыл бұрын
your peripherals sound horrible
@highwired77812 жыл бұрын
I've fallen in love with you
@GenjaOrigins3 жыл бұрын
Very Good i had some problems on learning how to use Eclipse but very good video. I need examples.
@GenjaOrigins3 жыл бұрын
But why you just dont return letters?
@pokemonbakogan74423 жыл бұрын
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
@blairgroove13022 жыл бұрын
I wish I was as skillful as you are
@swedishguyonyoutube46842 жыл бұрын
Much appreciated! /First year CS student!
@volkancamas9862 жыл бұрын
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;
@rikkoo Жыл бұрын
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?