0

need help quick. Output the input backwards.

Heres my code; package javaapplication126; import java.util.Arrays; import java.util.Scanner; public class JavaApplication126 { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Please Enter a word"); Scanner Reply1= new Scanner(System.in); //this is object for input String a=Reply1.next(); //this is renaming the input to a String[] Nope = new String[a.length()]; // stating that length of array System.out.println(Arrays.toString(Nope)); // converting input/array to String. //my output is [null, null, null] when i insert the word 2. // im trying to reverse the word entered and then to display it in its reversed form using arrays. How do i solve the null issue }

18th Apr 2017, 10:57 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
3 Answers
+ 4
if u wanna use arrays...check this code Here we split each character of string and store it in an array https://code.sololearn.com/cSOPlk2eNsYf/?ref=app
18th Apr 2017, 11:25 AM
Manideep
Manideep - avatar
+ 2
You can reverse a string in another way to by using one of the string functions //charAt().. check my below code https://code.sololearn.com/cNtNp5Nd78Rq/?ref=app
18th Apr 2017, 11:16 AM
Manideep
Manideep - avatar
+ 2
public static String reverse(String str) { StringBuilder sb = new StringBuilder(); for (int i = str.length(); i != 0; ) sb.append(str.charAt(--i)); return sb.toString(); } use of it: { String test = reverse("Hello world!"); System.out.println(test); //results: !dlrow olleH } Do not forget to use: str = reverse(str); Instead of just using: reverse(str); If reversing a variable. Done by array is like: char[] chars = {readline}.toCharArray(); for (int i = chars.length(); i != 0; ) System.out.print(chars[--i]); System.out.println(); //Convering String --> char[] //Counting down on char indexes // - print char from array //Breaking line https://code.sololearn.com/c4wehrc3Bepo
18th Apr 2017, 11:23 AM
Magyar DĂĄvid
Magyar DĂĄvid - avatar