B4J Question Change character separator when printing arrays to file

ziovec

Member
Licensed User
Hello there!

I have a list of arrays that gets printed on a txt file, and looks like this:

[first_element, second element, third element,... nth element]
[first_element, second element, third element,... nth element]
[first_element, second element, third element,... nth element]

Each row contains the same numbers of element inside the array.

This file is later read by another piece of code wich extract every element and do stuff with them.

Problem is that it could be possible that an element contains the character "," resulting in a bad reading of this file.
I have no control on how the elements are made because I grab them through various dB tables not made by me.

Question is: is it possible to change the character separator of an array from "," to something else I prefer like "|"?
Otherwise, could you suggest me a clever way to print out a list of arrays? ?

Thanks in advance!
 

Midimaster

Active Member
Licensed User
at first an additional question:

Do you combine the elements to an array? Or do you already receive the complete lines with commas already inside?
 
Upvote 0

ziovec

Member
Licensed User
StringUtils.SaveCSV
Tryed, but I got an error:

Log:
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class [Ljava.lang.String; (java.util.ArrayList and [Ljava.lang.String; are in module java.base of loader 'bootstrap')

at first an additional question:

Do you combine the elements to an array? Or do you already receive the complete lines with commas already inside?
I do combine those elements in an array and then add the arrays into a list.
 
Upvote 0

Midimaster

Active Member
Licensed User
I know, Erel will not like the code, but this show an "old school" way to build and separate strings from arrays with a "|" (Pipe)-Separator.

As you can see in my sample code, the comma signs in the cells are not touched, not removed or not miss-interpreted by the converting procedure:
B4X:
Sub Button1_Click
    Dim Test() As String=Array As String("A, AAA","B",",C ,D,")
    
    'how to convert the array into a string:
    Dim P As String =ArrayToString(Test)
    Log("OneString = <" & P & ">")
    
    'now convert the string back to a array:
    Dim result() As String=StringToArray(P)
    For i=0 To result.Length-1
        Log(i & ".element = <" & result(i) &">")
    Next
End Sub


Sub ArrayToString (locArray() As String) As String
    Dim OneString As String
    If locArray.Length=0 Then Return ""
    If locArray.Length=1 Then Return locArray(0)
    OneString=locArray(0)
    For i=1 To locArray.Length-1
        OneString=OneString & "|" & locArray(i)
    Next
    Return OneString
End Sub


Sub StringToArray (OneString As String ) As String()
    Dim locArray() As String
    If OneString.Contains("|")=True Then
        locArray=Regex.Split("\|",OneString)
    Else
        locArray= Array As String(OneString)
    End If
    Return locArray
End Sub
 
Upvote 0

ziovec

Member
Licensed User
I know, Erel will not like the code, but this show an "old school" way to build and separate strings from arrays with a "|" (Pipe)-Separator.

As you can see in my sample code, the comma signs in the cells are not touched, not removed or not miss-interpreted by the converting procedure:

Very clever, but I don't think this could work with my case....I'll try to explain better :D

Users select stuff from a table and, by pressing a button, adds those item to a "cart".
B4X:
Private Sub Button_click (Params as Map)
[.....]
    Order.AddAll(Array As String(variable1, variable2, variable3, variable4, variable5))
    ListedOrder.Add(Order)
[.....]
End Sub
Order and ListedOrder are Lists, variableN are Strings.
N.B: I need to have ListedOrder because it's going to be used to populate an html table in a static html page.
I tried populate said table with Order but I'll get each single character as a separated colum....

Later on, as the user reviewed their cart, eventually removing undesiderable items, the order gets printed to a txt file.

B4X:
 File.WriteList(File.DirApp, "file.txt", ListedOrder)
(simplified, but it gets the idea).

I tried with
B4X:
    Dim su As StringUtils
    su.SaveCSV(File.DirApp, "file.csv", "|", ListedOrder)
But I've got the error mentioned in previous message.


@Midimaster your solution, as I said, is very clever, but I think it doesn't suits me.
I mean, I could work it out...I'll try :D
Updates coming soon!
 
Upvote 0

ziovec

Member
Licensed User
I know, Erel will not like the code, but this show an "old school" way to build and separate strings from arrays with a "|" (Pipe)-Separator.

As you can see in my sample code, the comma signs in the cells are not touched, not removed or not miss-interpreted by the converting procedure:
Ok, rearranged something...and it works as intended!
Thanks a lot, I owe you one! ?
 
Upvote 0
Top