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;
    }
}
3
  • The error could be caused by an illegal argument. The Docs for Arrays.toString() specify an Object[] argument, not ArrayList. Perhaps try Arrays.toString(result.toArray()) ? Commented Dec 28, 2015 at 18:33
  • What data in your array? Commented Dec 28, 2015 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)? Commented Dec 28, 2015 at 22:30

5 Answers 5

3
Arrays.toString()

method accepts arrays not List.

Sign up to request clarification or add additional context in comments.

1 Comment

problem was that, so thanks.ı converted to arraylıst to strıng now
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 :)

1 Comment

So you suggest to call toString on an array? You might want to give it a try, to see what you get.
1

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

userC = TextUtils.join("\t", result);

1 Comment

Please avoid posting stuff like "thank you" in your answer. I edited your answer a bit, to improve the text.
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

Comments

-2

You need to first convert the List to array

ArrayList<String> result=new ArrayList<>();
String new_array=result.toArray().toString();

3 Comments

So you suggest to call toString on an array? You might want to give it a try, to see what you get.
thanks, found another way too, String userC = TextUtils.join("\t", result). İt easily converts arraylist to strıng
@busrao You might want to add this approach as another answer for this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.