2

I have a string array. I want to compare this string array with two string, so first i assigned array to variable then I compare this variable with strings.if array converted equals first string, do something else equals second, do something. But it raises cannot resolve method toString error.

String stopC = "stop";  //first strıng
String fastC = "fast";  //second strıng
String userC;   

switch (requestCode) {
    case REQ_CODE_SPEECH_INPUT: {
        if (resultCode == RESULT_OK && null != data) {
            /*strıng array*/            
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            userC = Arrays.toString(result); //array convert to strıng.gıves error here.

            if(userC.compareTo(fastC)==0){   //compare coverted array wth strıng
                exam.speedUpBall();
                Toast.makeText(this, "fast!! =)",
                        Toast.LENGTH_LONG).show();
            }

           else if (userC.compareTo(stopC)==0) {  
                exam.stop();
                Toast.makeText(this, "stop!! =)",
                        Toast.LENGTH_LONG).show();
            }

        }
        break;
    }
}
Cœur
  • 34,719
  • 24
  • 185
  • 251
busra o
  • 41
  • 5
  • The error could be caused by an illegal argument. The Docs for [`Arrays.toString()`](http://developer.android.com/reference/java/util/Arrays.html#toString(java.lang.Object[])) specify an `Object[]` argument, not `ArrayList`. Perhaps try `Arrays.toString(result.toArray())` ? – PPartisan Dec 28 '15 at 18:33
  • What data in your array? – Evgeniy Strepetov Dec 28 '15 at 18:36
  • @JarrodRoberson Since I'm maybe just stupid: can you please explain to me where you see the equality of "Object[] -> String" (OPs question) and "Object[] -> String[]" (duplicate)? – Tom Dec 28 '15 at 22:30

5 Answers5

3
Arrays.toString()

method accepts arrays not List.

Tom
  • 15,514
  • 17
  • 42
  • 51
Azat Nugusbayev
  • 1,321
  • 10
  • 19
1

The toString() of Java Arrays has to be applied on Arrays, and not for ArrayList.

So convert ArrayList to Array, then convert Array to String.

Try this code-

userC = result.toArray().toString();

It will work as expected :)

Manoj Kumar
  • 131
  • 1
  • 1
  • 7
  • 1
    So you suggest to call `toString` on an array? You might want to give it a try, to see what you get. – Tom Dec 28 '15 at 18:41
1

Another way to convert an ArrayList to String is by using TextUtils#join:

userC = TextUtils.join("\t", result);
Tom
  • 15,514
  • 17
  • 42
  • 51
busra o
  • 41
  • 5
  • Please avoid posting stuff like "thank you" in your answer. I edited your answer a bit, to improve the text. – Tom Dec 28 '15 at 19:19
0

According to the Documentation,

toString(boolean[] a)
Returns a string representation of the contents of the specified array.

So the arguments should be only arrays e.g. char[], boolean[], int[] etc.
ArrayList is not an array.

If you want to convert an ArrayList to String, you should build it yourself. For example:

ArrayList<String> resultArrayList =  data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String result = "";
for (String content: resultArrayList) {
    result += content;
}

Then you'll have a String that can be compared. Also, take a look at this topic here in SO

Best way to convert an ArrayList to a string

Community
  • 1
  • 1
rafaelc
  • 52,436
  • 15
  • 51
  • 78
-2

You need to first convert the List to array

ArrayList<String> result=new ArrayList<>();
String new_array=result.toArray().toString();
Nishant Srivastava
  • 4,625
  • 2
  • 23
  • 34
  • So you suggest to call `toString` on an array? You might want to give it a try, to see what you get. – Tom Dec 28 '15 at 18:41
  • thanks, found another way too, String userC = TextUtils.join("\t", result). İt easily converts arraylist to strıng – busra o Dec 28 '15 at 18:47
  • @busrao You might want to add this approach as another answer for this question. – Tom Dec 28 '15 at 18:55