+ 2

Reversing array in Java

Why is wrong? I want to get a reversing array public String getArrayReverse(int[] array) { String result = "" System.out.println("Original array: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("Array in reverse order: "); //Loop through the array in reverse order for (int i = array.length-1; i >= 0; i--) { System.out.print(array[i] + " "); }

6th Nov 2021, 2:27 PM
Rine
10 Réponses
+ 5
import java.util.Scanner; public class Program { public static void main(String[] args) { String str = new Scanner(System.in).nextLine(); System.out.println(new StringBuilder(str).reverse()); } } // Keep learning & happy coding :D https://code.sololearn.com/c2YhalWK4iqL/#java
6th Nov 2021, 2:56 PM
SoloProg
SoloProg - avatar
+ 2
Nounou These are two codes with the solution you can choose one both will work.
6th Nov 2021, 6:53 PM
SoloProg
SoloProg - avatar
6th Nov 2021, 2:51 PM
Rine
+ 1
You are declaring method within another method ,which is not allowed in java. You need more changes to work the code. But here am giving a working code of your code. public class Program { public static void main(String[] args) { char array[]="hello world".toCharArray(); //string to charecter array convertion //instead of string ,take here a input string. so edit this code to accept input and work with it. System.out.println("Original array: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i]);//+ " "); } System.out.println("\nArray in reverse order:"); //Loop through the array in reverse order for (int i = array.length-1; i >= 0; i--) { System.out.print(array[i]);//+ " "); } } }ll
6th Nov 2021, 3:20 PM
Jayakrishna 🇮🇳
+ 1
if you want to create a method and call it from main method then you need to do like this: public class Program { public static void main(String[] args) { getArrayReverse();//calling method here } //method defination public static void getArrayReverse(){ //add defination or instruction here } } //code above is not meant for code coach here.
6th Nov 2021, 3:32 PM
Jayakrishna 🇮🇳
+ 1
⭐Nakul Pasi S/0 Krishnalal Pasi⭐ you are spamming of this thread.. pls don't do
8th Nov 2021, 10:44 AM
Jayakrishna 🇮🇳
0
Can you show the full code? Save in playground and share link here.. Since you are passing int array but working as string array...
6th Nov 2021, 2:47 PM
Jayakrishna 🇮🇳
0
How can i change it to string because i want to work with string
6th Nov 2021, 2:51 PM
Rine
0
SoloProg should i put your code befor my code or what i don’t understand because when i run it i didnt get what i want please answer me and thank you for your time
6th Nov 2021, 3:14 PM
Rine