0

I'm writing a program that lets the user input a state, and it will echo the state entered, along with the state bird and flower.I'm getting an ArrayOutOfBounds exception, and I don't know how this is occuring. This happens at the printArray method

public class stateInformation
{
public static void searchState(String state)
{
    int row, col;
    String[][] states =
        {
            {"Alabama","Camellia","Yellowhammer"},
            {"Alaska","Forget-Me-Not","Willow Ptarmigan"},
            {"Arizona","Saguaro Cactus","Cactus Wren"},
            {"Arkansas","Apple blossom","Mockingbird"},
            {"California","Golden Poppy","California Valley Quail"},
            {"Colorado","Rocky Mountain Columbine","Lark Bunting"},
            {"Connecticut","Mountain Laurel","Robin"},
            {"Delaware","Peach Blossom","Blue Hen Chicken"},
            {"Florida","Orange Blossom","Mockingbird"},
            {"Georgia","Cherokee Rose","Brown Thrasher"},
            {"Hawaii","Hibiscus","Nene"},
            {"Idaho","Syringa","Mountain Bluebird"},
            {"Illinois","Native Violet","Cardinal"},
            {"Indiana","Peony","Cardinal"},
            {"Iowa","Wild Rose","Eastern Goldfinch"},
            {"Kansas","Native Sunflower","Western Meadowlark"},
            {"Kentucky","Goldenrod","Cardinal"},
            {"Louisiana","Magnolia","Eastern Brown Pelican"},
            {"Maine","Pine Cone & Tassle","Chickadee"},
            {"Maryland","Black Eyed Susan","Baltimore Oriole"},
            {"Massachusettes","Mayflower","Chickadee"},
            {"Michigan","Apple Blossom","Robin"},
            {"Minnesota","Lady Slipper","Common Loon"},
            {"Mississippi","Magnolia","Mockingbird"},
            {"Missouri","Hawthorn","Bluebird"},
            {"Montana","Bitterroot","Western Meadowlark"},
            {"Nebraska","Goldenrod","Western Meadowlark"},
            {"Nevada","Sagebrush","Mountain Bluebird"},
            {"New Hampshire","Purple Lilac","Purple Finch"},
            {"New Jersey","Purple Violet","Eastern Goldfinch"},
            {"New Mexico","Yucca","Roadrunner"},
            {"New York","Rose","Bluebird"},
            {"North Carolina","Dogwood","Cardinal"},
            {"North Dakota","Wild Prairie Rose","Western Meadowlark"},
            {"Ohio","Scarlet Carnation","Cardinal"},
            {"Oklahoma","Mistletoe","Scissor-Tailed Flycatcher"},
            {"Oregon","Oregon Grape","Western Meadowlark"},
            {"Pennsylvania","Mountain Laurel","Ruffed Grouse"},
            {"Rhode Island","Violet","Rhode Island Red"},
            {"South Carolina","Yellow Jessamine","Great Carolina Wren"},
            {"South Dakota","Pasque Flower","Ring-Necked Pheasant"},
            {"Tennessee","Purple Iris","Mockingbird"},
            {"Texas","Texas Blue Bonnet","Mockingbird"},
            {"Utah","Sego Lily","American Seagull"},
            {"Vermont","Red Clover","Hermit Thrush"},
            {"Virginia","Dogwood","Cardinal"},
            {"Washington","Western Rhododendron","Willow Goldfinch"},
            {"West Virginia","Rhododendron","Cardinal"},
            {"Wisconsin","Wood Violet","Robin"},
            {"Wyoming","Indian Paint Brush","Western Meadowlark"},
        };


for(row=0;row<states.length;row++)
{
    for(col=0;col<states[row].length;col++)
    {
        if(states[row][col].equals(state))
        {
            printArray(states,row);
        }
    }
}




}
public static void printArray(String[][] array, int row)
{
    for(int i=0;row<array[row].length;i++)
    {
        System.out.println(array[row][i]);
    }
}

public static void main(String[] args)
{
    searchState("Arizona");
}
}           
Matt
  • 73
  • 11

1 Answers1

1

Check the loop in your main method here row will always be < array[row].length and due to infinite loop you reached to an array index which is not exist and you are accessing it so it gives this error. Hope the answer will be clear

Aammad Ullah
  • 274
  • 4
  • 11