0

I'm writing a mobile application in Android studio. I have an arrayList that i'm converting into a string to be evaluated in a script engine.

I am currently using

    resultText = total.stream().collect(Collectors.joining(""));

With total being

    ArrayList<CharSequence> total = new ArrayList<>();

The current input output is something like this

    CharSequence [1,+,2] --> .Stream() "1+2" --> .eval 3.0

Is there an alternative method to get this output? I'm currently using API level 28 to have access to stream, but i require a min API level of 19.

The .ToString() method does not get the required output.

    CharSequence [1,+,2] --> .ToString "[1,+,2]" --> .eval NULL

Any help would be greatly appreciated.

  • If you are you talking about `java.lang.CharSequence` then `total.toString()` will do what you want. – Klitos Kyriacou Apr 03 '19 at 06:58
  • You say you are converting an `ArrayList` to a `String` in the first paragraph, and then you say `total` is a `CharSequence`. So, which is it – an `ArrayList` or a `CharSequence`? – Klitos Kyriacou Apr 03 '19 at 11:16
  • total is an ArrayList. Ill update the question to make it more clear. total.ToString preserves the square brackets and the commas within the ArrayLists structure and will fail to be evaluated by the ScriptEngine. – Jack Bonnnell Apr 03 '19 at 11:57

2 Answers2

3

You can use the following :

resultText = total.toString(); // where total is CharSequence Array.

your eval function should work with this.

dpk
  • 641
  • 1
  • 5
  • 15
1

You can use TextUtils class.

List<Character> list = new ArrayList<>(Arrays.asList(
        '1', '+', '2'
));

String result = TextUtils.join("", list);

Base od documentation method join() was Added in API level 1

Boken
  • 4,825
  • 10
  • 32
  • 42