+ 1

I need help to reverse a string with Java when a user input something, I cant solve this exercise

The exercise ask me to reverse a string from an array using arr.length the was input by user: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int i = arr.length-1; i < 0; i--) {System.out.println(arr[i]);} } }

5th May 2021, 1:59 PM
Vitor
3 Antworten
+ 4
Vitor , before we are going to help you, you should show your attempt. so please put your code in playground and link it here. as a general hint to you, you can reverse a string, by using a ▪︎StringBuffer but it looks that there is also some information missing, as your tags are: array, reverse, string try to find more about this in reading the java docs
5th May 2021, 2:49 PM
Lothar
Lothar - avatar
0
What is the problem? Type conversion? Int cannot convert to String: import java.util.Scanner; public class Converter { public static int toBinary (int num) { String binary = ""; while(num > 0) {binary= (num %2) + binary; num /= 2; } return binary;} } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } }
8th May 2021, 11:24 PM
Vitor
5th May 2021, 3:22 PM
born2code
born2code - avatar