Back Reverse Strings in JAVA | (simple & easy)

0-3
Created By: Mintype
Published At: 2022-12-07
Search Query: Java Create Write Files,Java Get Started,Java Output,Java Strings,Java User Input
Tags: how to reverse string in java reverse string java reverse string in java reverse a string in java reverse string java program reverse string program in java with explanation java program java program to reverse a string
Reverse Strings in JAVA | (simple & easy)
In today's video, I will be showing you how to reverse a string in Java!

β–Ί Software Used:
● IntelliJ IDEA: https://www.jetbrains.com/idea/download/

β–Ί Helpful Learning Sites:
● Codecademy: https://www.codecademy.com/
● freeCodeCamp: https://www.freecodecamp.org/

β–Ί Song Used:
● One More Time: https://youtu.be/MTZ5G0qVQ0I

β–Ί Support or Follow me:
● Join our Discord: https://discord.gg/hhGu6mV5wm
● GitHub: https://github.com/SmallPlayz
● Twitter: https://twitter.com/SmallPlayz_
● Instagram: https://www.instagram.com/smallplayz_/
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@kevswags
2023-09-18

I think i found an easier way:
String s = "123456789";
StringBuilder x = new StringBuilder(s);
x.reverse();
String output = x.toString();

System.out.println(output);

@mintype

Your way works and is more simple than my way, however this video is supposed to teach those who don’t know how to reverse a sting WITHOUT StringBuilder. (For example in a AP CSA class they would ask you to do this without StringBuilder)

VidTutorAssistant (GPT-4)

Hey there @kevswags! Thanks for sharing your approach. Indeed, using StringBuilder's reverse() method is a clever way to simplify this problem - such is the beauty of Java's vast standard libraries. It is definitely an easier method for those who are familiar with these libraries, and it's neat in its own right. However, my purpose in the video was to demonstrate the underlying logic of how a string can be reversed for those who might be new to Java or even programming in general, and in such cases, a straightforward loop could be more digestible. As one progresses, they can then appreciate the tools and methods that Java provides to simplify these processes. Keep those great ideas coming!


@nahidasultana6842
2023-09-01

Hi, I am having an error. what mistake i have made?
package Practice;



public class HelloReverse {



public static void main(String[] args) {



String input ="Hello world!";

String output=" ";

for(int i=input.length()-1;i>=0;i--);

output+=input.charAt(i);
System.out.println(output);

@mintype

You have a ; (semicolon) at the end of your for loop!!!!
updated code:

String input ="Hello world!";
String output=" ";
for(int i=input.length()-1;i>=0;i--) output+=input.charAt(i);
System.out.println(output);

VidTutorAssistant (GPT-4)

Hi @nahidasultana6842! Looks like you're almost there. The issue you are facing is due to the placement of the semicolon (;) at the end of your for loop declaration. Here's what's happening: the semicolon is signifying the end of the for loop, so your program never really gets to the line where you add the character to your output string. Try removing that semicolon after your for loop and your code should work as expected.

So it should look something like this:

public class HelloReverse {

public static void main(String[] args) {

String input ="Hello world!";
String output="";
for(int i=input.length()-1;i>=0;i--){
output+=input.charAt(i);
}
System.out.println(output);
}
Keep coding! Happy Java programming! πŸš€


@bankarsandipk
2023-07-24

Nice short explanation 😊

@mintype

Glad it was helpful!

VidTutorAssistant (GPT-4)

Thanks a lot @bankarsandipk! 😊 Your kind words always motivate us to create more informative and accessible content. Stay tuned to Mintype for more 'byte-sized' Java programming lessons! Happy coding!