How do I convert a YailList to String and replace double quotes with single quotes?

I am trying to send a list to my extension, which then needs to be converted to a string, and then any double quotes need to be replaced with single quotes. The extension constructs an html file as a string, which is why I do not need/want to use the list directly. This is what I have so far (just the code for this):

public void NewDrawLineChart( YailList arrayTable) {

        String arrayString = arrayTable.toString();
        arrayString = arrayString.replaceAll("\"", "'");

but it doesn't work for me.

I also tried this:

public void NewDrawLineChart( YailList arrayTable) {

        String arrayString = Arrays.toString(arrayTable.toArray());
        arrayString = arrayString.replaceAll("\"", "'");

without success...

I can do this in the blocks using a procedure, and this "works", but obviously it would be better to handle in the extension:

Resources:

http://3nportal.com/AIBridge/API/com/google/appinventor/components/runtime/util/YailList.html

Hmmm....thank you for your help....

Using my example above:

Input (a list):

[
["Year", "Sales", "Expenses", "Tips"], 
["2004", 1000, 400, 50], 
["2005", 1200, 500, 25], 
["2006", 900, 300, 30]
]

Expected Output (a string):

"[
['Year', 'Sales', 'Expenses', 'Tips'],
['2004', 1000, 400, 50],
['2005', 1200, 500, 25],
['2006', 900, 300, 30]
]"

This works for me:

public static void main(String[] args) {
        System.out.println(Arrays.toString(parse(new Object[]{"\"hello\"", 1,Arrays.toString(parse(new Object[]{"\"hello\"", 1}))})));
}
 public static Object[] parse(Object[] arr){
        Object[] rArr = new Object[arr.length];
        for (int i=0;i<arr.length;i++){
            Object o = arr[i];
            if (o instanceof String){
                rArr[i] = o.toString().replaceAll("\"","'");
            }else if(o instanceof Object[]){
                Object[] innerArr = (Object[]) o;
                rArr[i] = parse(innerArr);
            }else {
                rArr[i] = o;
            }
        }
        return rArr;
 }
Output: ['hello', 1, ['hello', 1]]

@oseamiya I appreciate your efforts but there is a small flaw in your code and that's what if user inputs an Array of Object Arrays (not String Array).

2 Likes

Also tried this, without success

public void NewDrawLineChart( YailList arrayTable) { 

    String arrayString = Arrays.toString(arrayTable.toArray()); 
    arrayString = arrayString.replaceAll("\"", "'");

Looks like I will need to iterate over the yailist :frowning:

I found the java code for the join block in the sources, this simply uses .toString() to convert a list to a string in the blocks, so was hoping it might have as straight forward in java code....

Mmm...

Here is my code Tim:

@SimpleFunction(description = "")
  public String Replacement(Object list) {
    StringBuilder builder = new StringBuilder();

    Object[] array = list instanceof YailList ?
            ((YailList) list).toArray() : (Object[]) list;

    for(Object object: array) {
      if(!(object instanceof List)) {
        if(object instanceof String) {
          builder.append("'").append(object).append("'");
        } else {
          builder.append(object);
        }
      } else {
        builder.append(Replacement(object));
      }
      builder.append(", ");
    }


    String result = builder.toString();

    if(result.endsWith(", ")) {
      result = result.substring(0, result.length() - 2);
    }

    return "[" + result + "]";
  }

And here is the result:

Thank you @Kumaraswamy. I am not trying to return the string to the app, but perhaps you were demonstrating your code that way :wink:

I have not seen anything yet that shows me how to take the yaillist input and apply it to the code?

e.g.

public void NewDrawLineChart( YailList arrayTable) { 

    do something to **arrayTable**

I also do not understand why the built-in functions for "yaillist" do not appear to be working in my build...

http://3nportal.com/AIBridge/API/com/google/appinventor/components/runtime/util/YailList.html

I have yaillist in my imports

import com.google.appinventor.components.runtime.util.YailList;

YailList is an object that can be only created or the elements inside are just readable.

That means you can only read elements from it.

Object[] list = yailList.toArray(); // converting YailList to array
ArrayList<Object> objects = new ArrayList<>(Arrays.asList((yailList).toArray()));
// Object arrayList

If you're trying to add an element then it's not possible. Instead, you can convert the normal list to YailList.

1 Like

What about this Sir @TIMAI2 ?

You will have to change just few things.

Wouldn't know where to start :wink:

example code starting from here might help me ....

public void NewDrawLineChart( YailList arrayTable) { 
    
do something to **arrayTable**

Just like this:

// convert YailList to Object[]
Object[] oArr = yailList.toArray();

// convert Object[] to YailList
YailList yailList = YailList.makeList(oArr);

OK, so I convert my yaillist "arrayTable" to an array:

Object[] oArr = arrayTable.toArray();

now I want that array as a string ?

String arrayString = Arrays.toString(oArr);

then I can carry out the replace?

String rString = arrayString.replaceAll("\"", "'");

If this is correct, how do I see my strings arrayString/rString to ensure that the format has been retained?

(I am using intelliJ for IDE)

No.
Now you have to call parse method with that array as argument which will return an array that you have to convert to YailList.

Java is fun, sometimes. :grin:

rarely....it is like trying to catch a black cat in a dark room, when the cat isn't even there :slight_smile:

Let me simplify it.

I just realized that "string" and ""string"" are not same.
So it is not possible to replace " with ' in a string object (because we can't call it a string then).

If you want to replace latter:

public YailList RMethod(YailList yailList){
        Object[] o = parse(yailList.toArray());
        return YailList.makeList(o);
    }
   public Object[] parse(Object[] arr){
        Object[] rArr = new Object[arr.length];
        for (int i=0;i<arr.length;i++){
            Object o = arr[i];
            if (o instanceof String){
                rArr[i] = o.toString().replace("\"","'");
            }else if(o instanceof Object[]){
                Object[] innerArr = (Object[]) o;
                rArr[i] = parse(innerArr);
            }else {
                rArr[i] = o;
            }
        }
        return rArr;
    }
1 Like

Odd, these blocks do it:

image

and they must come from java code ?

I don't think so.


Print the result on a lable.If it shows 'string' then it means it is actually "'string'". Java/Android does not show " for a string.

I can pass the output of that procedure as a string to the extension and it generates a chart as expected.

I am just working up an example....

See attached a working aia project for a pie chart, and the java code used to build the extension. Note: this uses the blocks procedure to convert the AI2 list to the correct string.

pieChart.aia (7.9 KB)

Pie.java.txt (3.2 KB)

also Google Charts pie chart link:
https://developers.google.com/chart/interactive/docs/gallery/piechart