Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. QtonPi
  4. In Java, you may convert a string to an array.
QtWS25 Last Chance

In Java, you may convert a string to an array.

Scheduled Pinned Locked Moved Unsolved QtonPi
java
4 Posts 4 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin Bhatt
    wrote on 1 Dec 2022, 09:14 last edited by
    #1

    I have a string array that says "ABCD", "EFGH", "IJKL", and "MNOP". I'm reversing the array as well as individual strings inside the array, and I expect the result to be an array of strings, but the output is a string.
    My code is shown below.

    public class ReverseString {
        String s = "";
    
        public static void main(String[] args) {
            ReverseString rs = new ReverseString();
            String value = "";
            String arr[] = { "ABCD", "EFGH", "IJKL", "MNOP" };
            for (int i = arr.length - 1; i >= 0; i--) {
                value = rs.reverseArray(arr[i]);
            }
            System.out.println(value);
        }
    
        public String reverseArray(String arr1) {
            for (int k = arr1.length() - 1; k >= 0; k--) {
                s += arr1.charAt(k);
            }
            return s.toString();
        }
    }
    

    and the result is PONMLKJIHGFEDCBA
    An online site like this suggests saving the return of reverseArray rather than simply reporting it. Is that right? How do I convert it back to an array?

    J 1 Reply Last reply 1 Dec 2022, 09:21
    0
    • S Sachin Bhatt
      1 Dec 2022, 09:14

      I have a string array that says "ABCD", "EFGH", "IJKL", and "MNOP". I'm reversing the array as well as individual strings inside the array, and I expect the result to be an array of strings, but the output is a string.
      My code is shown below.

      public class ReverseString {
          String s = "";
      
          public static void main(String[] args) {
              ReverseString rs = new ReverseString();
              String value = "";
              String arr[] = { "ABCD", "EFGH", "IJKL", "MNOP" };
              for (int i = arr.length - 1; i >= 0; i--) {
                  value = rs.reverseArray(arr[i]);
              }
              System.out.println(value);
          }
      
          public String reverseArray(String arr1) {
              for (int k = arr1.length() - 1; k >= 0; k--) {
                  s += arr1.charAt(k);
              }
              return s.toString();
          }
      }
      

      and the result is PONMLKJIHGFEDCBA
      An online site like this suggests saving the return of reverseArray rather than simply reporting it. Is that right? How do I convert it back to an array?

      J Offline
      J Offline
      JonB
      wrote on 1 Dec 2022, 09:21 last edited by JonB 12 Jan 2022, 15:09
      #2

      @Sachin-Bhatt said in In Java, you may convert a string to an array.:

      and I expect the result to be an array of strings, but the output is a string.

      So this is a question about Java, not Qt, right? And you have example Java code but you'd like someone to comment on/modify it?

      I have never used Java, but I think a 30 second glance at your code indicates it is printing a string because you go System.out.println(value); after the loop, and that will be the last value which value (a String) happened to take in the loop? Surely you want to print out the reversed array you have just constructed, System.out.println(rs);, don't you??

      EDIT
      No, that's not right. Let me try again.

      I'm reversing the array as well
      I expect the result to be an array of strings

      I don't see that. Where do you think you are reversing (the elements in) String arr[]? You are iterating through them in reverse array order, but nothing stores those into either the original arr[] or any new array? That's what I see? To actually put the results into an array you would (presumably) need to save the returned result from reverseArray() into a (new) array as you go through the loop.

      and I expect the result to be an array of strings, but the output is a string.

      I am confused. You only print value outside the loop, yet it contains all the characters from all the elements? You have value = rs.reverseArray(arr[i]); inside the loop, you sure it is not value += rs.reverseArray(arr[i]);?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomZ
        wrote on 1 Dec 2022, 14:05 last edited by
        #3

        You define your value as a string, which is where the result is collected.
        Don't want a string, fix the software.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Harish118
          Banned
          wrote on 1 Jul 2024, 11:34 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved