-1
    class Reverse_Word
    {
        public static void main(String args[])
        {
            String s = "This is a program";
            String str[] = s.split(" ");
            String wrd = new String();
            try{
            for(int i=0;i<str.length;i++)
            {

                wrd = str[i];
                for(int j=wrd.length();j>=0;j--)
                {
                    String str1[]=wrd.split("//");
                    System.out.println(str1);
                }

            }

            }catch(Exception e)
            {
                System.err.println(e);
            }
        }
    }

Now, when I modified the code. Their is no error but getting wrong output like some hashcode [Ljava.lang.String;@d70d7a [Ljava.lang.String;@b5f53a [Ljava.lang.String;@1f6f0bf [Ljava.lang.String;@137c60d [Ljava.lang.String;@ab853b [Ljava.lang.String;@b82368 [Ljava.lang.String;@11c8a71 [Ljava.lang.String;@c53dce [Ljava.lang.String;@15cda3f [Ljava.lang.String;@fc9944 [Ljava.lang.String;@1b26af3 [Ljava.lang.String;@8b819f [Ljava.lang.String;@eb017e [Ljava.lang.String;@aeffdf [Ljava.lang.String;@120a47e [Ljava.lang.String;@f73c1 [Ljava.lang.String;@789144 [Ljava.lang.String;@1893efe

  • possible duplicate of [What's the simplest way to print an array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array) – Raedwald Mar 05 '15 at 08:04

3 Answers3

0
 String str1 = "";// its just a String object
  str1[j] = wrd.split(""); // and you are using it as an array;

Changes required :

  String str1[];

    str1= wrd.split(""); // I dont know what is your purpose here

The code for your answer is

 StringBuilder newstring = new StringBuilder();
    try {
        for (int i = 0; i < str.length; i++) {
            // System.out.println(str[i]);
            wrd = str[i];
            System.out.println("Hi" + str[i]);
           char chars[] = wrd.toCharArray();// converting your string in chars
               for(int k=chars.length-1;k>=0;k--){
                   newstring.append(chars[k]); // appending your characters in reverse order
               } 
               newstring.append(' ');// appending `space` after each word


        }
    } catch (Exception e) {
        System.err.println(e);
    }
    System.out.println(newstring);// printing your entire reversed string
AJ.
  • 4,526
  • 5
  • 29
  • 41
0

Of course.. You have

String str1 = ""; // str1 is a String and not an array

and then you do

str1[j] = wrd.split("");// I am getting error here. because str1 is NOT an array

Change str1 to an array. String[] str1 = null;

Also, wrd.split("") does nothing

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

If you want to split the word to array of characters (an array of strings - each of which is one charatcer) you should use:

wrd.split("//");

So if wrd = "This"; it will return the array: {"T", "h", "i", "s"}.

Example:

String str = "abcd";
String[] arr = str.split("//");
System.out.println(Arrays.toString(arr)); // prints [abcd]

Note that since it returns an array of Strings - it will not fit into str1[j] (which is an error by itself since a string cannot be accessed like an array) - but I will let you solve this issue by yourself (unless you specifically ask for the complete logic).

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129