Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how do you change the datatype of an attribute from String to Array?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default how do you change the datatype of an attribute from String to Array?

    I'm having a difficulty. I want to modify an attribute's data type from String to Array.
    {
        “id”: “trn:tarb:tradingpartner:uuid:00000464-fa72-49fe-b050-c4bf1cfd8259”,
        “partnerNumber”:1098981”,
        “partnerName”: "AFLO ",
        “currencyCode”: “GBP”,
        “commercialPartnerStatus”: “Inactive”,
        “source”: {
            “id”: “STV0001$$”,
            “name”: “QS”
        },
        “corpCode”: “DR”,
        “subCorpCode”: “001”,
        “targetMarket”: “UK”
    }
    In the preceding one, I want to convert targetMarket from String to Array without affecting its name.
     “targetMarket”:[
        “UK”,
        “IN”
    ]
    The issue is with the old data, which is in string format, and if I alter the attribute in java, I receive an error while retrieving data from the database. It cannot automatically cast from string to ArrayList. I saw a method on this page but I'm confused is it compatible here or not? https://www.scaler.com/topics/string-to-array-in-java/
    As a result, I must first change the old data before changing the java code. It is also acceptable if data is null or removed after changing the object from string to array.

  2. The Following User Says Thank You to codingkeera For This Useful Post:

    Vithika Dutt (November 15th, 2022)

  3. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    117
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: how do you change the datatype of an attribute from String to Array?

    import java.util.*;
     
    class StringtoCharArray {
     
      public static void main(String args[]) {
        String str = "Scaler"; // Given String
     
        // Creating array of string length
        char[] arr = new char[str.length()];
     
        // Copy character by character into array
        for (int j = 0; j < str.length(); j++) {
          arr[j] = str.charAt(j);
        }
     
        // Printing the character array
        for (char x : arr) {
          System.out.println(x);
        }
      }
    }
    String to char array in Java. This is the program they give.
    just convert the string to int in java when you cast the str to int in Java. And try writing in, see if that works when using the program. String to char array.

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how do you change the datatype of an attribute from String to Array?

    It would be simpler to use the String class's toCharArray() method:
       String str = "Scaler"; // Given String
       char[] charA = str.toCharArray();
       System.out.println(Arrays.toString(charA));  // [S, c, a, l, e, r]
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to change a string at indexOf
    By justyStepi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 4th, 2013, 10:22 AM
  2. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  3. What is the easiest way to change a single character in a string?
    By Jumbosize in forum Java Theory & Questions
    Replies: 2
    Last Post: April 26th, 2012, 04:16 PM
  4. Can I change attributedstring into string?
    By star12345645 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2012, 08:02 AM
  5. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM

Tags for this Thread