Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8

2.8k Views Asked by At

hi dear here I am posting my whole class where I am getting problem. First I explain my problem. I am generating an arrayList "Items" using spinners and the elements in spinners and arrays are exactly same then After comparing array length and ArayList size I want to jump on next activity but problem is occoured in comparing arrays now please help me for this problem Thanks here is my activity

package com.example.mine4.pantryrecipes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.util.List;
import java.util.ArrayList;

import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;



public class AddItem extends Activity {
    MultiSelectionSpinner spinner1,spinner2,spinner3,spinner4;
    Button button1;
    TextView tv;
    private int count = 0;
    List<String> Items=new ArrayList<>();
    List<String> veg=new ArrayList<>();
    List<String> spice=new ArrayList<>();
    List<String> dairy=new ArrayList<>();
    ArrayList<Boolean> subset = new ArrayList<>();
    String[] arrayRecipe = { "chicken","vegetable oil","ginger",
            "onion", "garlic","potatoes","tomatoes","roasted peanuts", "peanut butter"};
    String[] arrayRecipe2 = { "chicken","garlic","vegetable oil", "tomatoes",
            "Dijon mustard","breadcrumbs","Parmesan cheese","unsalted butter"};
    String[] arrayRecipe3 = { "chicken", "vegetable oil","unsalted butter",
            "sugar", "garlic", "sauce"};
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        button1=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.tv);
        String[] arrayItems = {"chicken"};
        String[] arrayVeg = { "vegetable oil", "ginger",
                "garlic","potatoes" ,"tomatoes","onion"};
        String[] arraySpice = { "roasted peanuts", "sauce","Dijon mustard" };
        String[] arrayDairy = { "breadcrumbs", "Parmesan cheese", "unsalted butter","sugar", "peanut butter" };
        spinner1 = (MultiSelectionSpinner) findViewById(R.id.mySpinner1);
        spinner2 = (MultiSelectionSpinner) findViewById(R.id.mySpinner2);
        spinner3 = (MultiSelectionSpinner) findViewById(R.id.mySpinner3);
        spinner4= (MultiSelectionSpinner) findViewById(R.id.mySpinner4);

        spinner1.setItems(arrayItems);
        spinner2.setItems(arrayVeg);
        spinner3.setItems(arraySpice);
        spinner4.setItems(arrayDairy);

    }
    public void onClick(View v)
    {
        Items=spinner1.getSelectedStrings();
        veg=spinner2.getSelectedStrings();
        spice=spinner3.getSelectedStrings();
        dairy=spinner4.getSelectedStrings();
        Items.addAll(veg);
        Items.addAll(spice);
        Items.addAll(dairy);
//        for(int i=0;i<Items.size();i++)
//            tv.append(Items.get(i));

        compareArray();
        //count++;
    }
    public void compareArray()
    {
        for(int j = 0 ; j < arrayRecipe.length ; j++)
            {
                for(int i = 0 ; i <Items.size() ; i++)
                {
                    if((arrayRecipe[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe2[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe3[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                }
            }

        if(subset != null) {
            if ((arrayRecipe.length == subset.size())) {
                Intent nextClass = new Intent();
                nextClass.setClass(AddItem.this, TenIngreRec.class);
                startActivity(nextClass);
                finish();
                Toast.makeText(getApplicationContext(), "arrayRecipe", Toast.LENGTH_LONG).show();
            }
            if ((arrayRecipe2.length == subset.size())) {
                Intent nextClass = new Intent();
                nextClass.setClass(AddItem.this, EightIngreRec.class);
                startActivity(nextClass);
                finish();
                Toast.makeText(getApplicationContext(), "arrayRecipe2", Toast.LENGTH_LONG).show();
            }
            if ((arrayRecipe3.length == subset.size())) {
                Intent nextClass = new Intent();
                nextClass.setClass(AddItem.this, EightIngreRec.class);
                startActivity(nextClass);
                finish();
                Toast.makeText(getApplicationContext(), "arrayRecipe3", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Un-Matched", Toast.LENGTH_LONG).show();
            }
        }
    }
    @Override
    public void onBackPressed()
    {
        // code here to show dialog
        super.onBackPressed();  // optional depending on your needs
        Intent intn = new Intent(AddItem.this,Exmain.class);
        startActivity(intn);
    }
  
}

and the major problem occured in this code which is array indexOutOfBound

 for(int j = 0 ; j < arrayRecipe.length ; j++)
            {
                for(int i = 0 ; i <Items.size() ; i++)
                {
                    if((arrayRecipe[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe2[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe3[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                }
            }

thanks again for helping me

3

There are 3 best solutions below

0
On

array.Length() always count from 1 not form zero use arrayRecipe.length -1 in your for loop

0
On

Use This

 for(int j = 0 ; j < (arrayRecipe.length)-1 ; j++)
            {
                for(int i = 0 ; i <Items.size() ; i++)
                {
                    if((arrayRecipe[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe2[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                    if ((arrayRecipe3[i].equals(Items.get(j))))
                    {
                        subset.add(true);
                        break;
                    }
                }
            }

2
On

Two problems:

  1. You're using the size of arrayRecipe to bound the index of arrayRecipe, arrayRecipe2 and arrayRecipe3 All three arrays are different in size, hence leading to your error. You need 3 different for loops, one for each arrayRecipe.

  2. Your i and j are in the wrong places. Items.get(j) should be Items.get(i) as you are using Items.size to limit the size of i. Likewise, arrayRecipe.get(i) should be arrayRecipe.get(j).

Also, it is true that getLength() and getSize() start counting from 1 rather than 0 so the index of the final slot is (getLenght() - 1), but by using < as the clause in your for loop handles that.