0
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the number to reverse ");

    String mystring = sc.nextLine();    

    char[] my_3 = mystring.toCharArray();   // string converted into array

How to convert the char array back to string

    for (int i = my_3.length - 1; i >= 0; i--) {
        System.out.print(my_3[i]);
        String b = new String(my_3[i]);  
                   }
  • 1
    why not just `String reversed = new StringBuilder(mystring).reverse().toString();`? – Ousmane D. Apr 14 '18 at 08:59
  • 1
    i meant to say how can i store elements of array char in a string through a loop – Haseeb Ansari Apr 14 '18 at 09:06
  • @Aomine...He is asking how to convert a CharArray to a String!.... He's not asking how to reverse a string.. This question is not duplicate of what you mentioned above – letsintegreat Apr 14 '18 at 09:07
  • @HarshitSeksaria there are still many duplicates for that, I've added it to the list of duplicates. – Ousmane D. Apr 14 '18 at 09:08
  • @HaseebAnsari there are still many duplicates for that, I've added it to the list of duplicates. – Ousmane D. Apr 14 '18 at 09:09
  • @Aominè..I know that you earned Java Badge just yersterday, but it doesnt mean that you can do what you want... You should mention the right question of which this question is duplicate – letsintegreat Apr 14 '18 at 09:10
  • @HarshitSeksaria Completely the opposite. the OP code is reversing a string in a complicated manner so I suggested "Reverse a string in Java" and then the OP clarified they wanted to convert the char array back to a string which I have then added appropriate duplicates. – Ousmane D. Apr 14 '18 at 09:13
  • @Aominè... But he never asked how to reverse a string... Yes, You can tell him much simpler way to do so as a bonus for OP.. But you cant declare this duplicate of `Reverse a string in Java` – letsintegreat Apr 14 '18 at 09:19
  • @HarshitSeksaria so doesn't the other two duplicates answer the questions of **"How to convert the char array back to string"**? – Ousmane D. Apr 14 '18 at 09:20
  • @Aominè.. You added them after I ask you to do so – letsintegreat Apr 14 '18 at 09:21
  • @Aominè... And now you are changing the topic, so that anyone (who watch the comments for this question later) can't figure out your mistake! – letsintegreat Apr 14 '18 at 09:23

1 Answers1

1

Try this.

String mystring = "abcde";
String reverse = new StringBuilder(mystring).reverse().toString();
System.out.println(reverse); // -> edcba