+ 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] + " "); }
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
+ 2
Nounou
These are two codes with the solution you can choose one both will work.
+ 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
+ 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.
+ 1
⭐Nakul Pasi S/0 Krishnalal Pasi⭐ you are spamming of this thread.. pls don't do
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...
0
How can i change it to string because i want to work with string
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