2

I'm working on a program for a plant nursery that has two classes; PlantNursery and Plant. The user gets promoted 4 questions. 1) Add a plant, 2) List all the plants, 3) Edit a plant, and 4) Quit. I got 1, 2, and 4 working fine. My problem lies within option 3. I display the current list of plants in the array and ask the user to pick one by it's common name. Then I store the String and compare it with an if statement. The if statement is not working as expected. I get the error message:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at plantnursery.PlantNursery.main(PlantNursery.java:92)
C:\Users\diggz\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 52 seconds)

Plant class:

package plantnursery;

public class Plant 
{
    //Variables
    private String commonName, scientificName;
    private double maxHeight, price;
    private boolean fragile;

    //Constructor
    public Plant(String commonName, String scientificName, double maxHeight, 
            double price, boolean fragile)
    {
        this.maxHeight = maxHeight;
        this.price = price;
        this.commonName = commonName;
        this.scientificName = scientificName;
        this.fragile = fragile;
    }

    public double getMaxHeight()
    {
        return maxHeight;
    }

    public void setMaxHeight(double maxHeight)
    {
        this.maxHeight = maxHeight;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    public String getCommonName()
    {
        return commonName;
    }

    public void setCommonName(String commonName)
    {
        this.commonName = commonName;
    }

    public String getScientificName()
    {
        return scientificName;
    }

    public void setScientificName(String scientificName)
    {
        this.scientificName = scientificName;
    }

    public boolean isFragile()
    {
        return fragile;
    }

    public void setFragile(boolean fragile)
    {
        this.fragile = fragile;
    }

    @Override
    public String toString() {
        return "Plant{" + "commonName= " + commonName + ", scientificName= " 
                + scientificName + ", maxHeight= " + maxHeight + ", price= " 
                + price + ", fragile= " + fragile + '}';
    } 
}

PlantNursery class:

package plantnursery;

import java.util.ArrayList;
import java.util.Scanner;


public class PlantNursery
{

    public static void main(String[] args)
    {
        //Variables to hold user input.
        double userHeight, userPrice;
        String userComName, userSciName, blankLine;
        boolean userFragile;
        int ans, choice;
        //Reference variable to an object.
        Plant p;
        //Scanner for user input.
        Scanner scan = new Scanner(System.in);
        //ArrayList to store all the plants in.
        ArrayList<Plant> plantList = new ArrayList<>();

        //While loop asking the user to create new plants and store them
        //in the the ArrayList. Edit any of the plants already in the ArrayList
        //or quit the program.
        while(true)
        {
            //Ask the user what they want to do.
            System.out.println("What do you want to do?\n1. Add a plant. "
                    + "\n2. List all plants.\n3. Edit a plant. \n4. Quit.");
            //Store answer
            ans = scan.nextInt();

            //Choice 1. Add a new plant into the ArrayList.
            if(ans == 1)
            {
                //Get rid of buffer overflow from int ans.
                blankLine = scan.nextLine();

                //Ask the user for input for a new plant object.
                System.out.println("Please enter the common name of the plant:");
                userComName = scan.nextLine();
                System.out.println("Please enter the scienitific name of the plant: ");
                userSciName = scan.nextLine();
                System.out.println("Please enter the maximum height (in feet) of the plant: ");
                userHeight = scan.nextDouble();
                System.out.println("Please enter the price of the plant: ");
                userPrice = scan.nextDouble();
                System.out.println("Please enter if the plant is fragile (true or false): ");
                userFragile = scan.nextBoolean();

                //Create the new plant object.
                p = new Plant(userComName, userSciName, userHeight, userPrice, 
                        userFragile);

                //Add the plant object to the ArrayList.
                plantList.add(p);
            }

            //Choice 2. Display all plant(s) in the ArrayList.
            if(ans == 2)
            {
                //List all the current plants in the ArrayList.
                for(Plant curList : plantList)
                {
                    System.out.println(curList);
                }
            }

            //Choice 3. Edit information on plant(s) in ArrayList.
            if(ans == 3)
            {
                //Allows the user to edit until they wish to quit.
                while(true)
                {
                    //Counter for ArrayList
                    int i;
                    //String to hold which plant the user wishes to edit.
                    String userAns;
                    //Ask the user which plant they wish to edit.
                    System.out.println("Which plant to wish to edit (choose the common name)?");
                    //Display the plant(s).
                    for(i = 0; i < plantList.size(); i++)
                    {
                        System.out.println(plantList.get(i));
                    }   
                    //Get the user input and compare it to the Common Name
                    blankLine = scan.nextLine();
                    userAns = scan.nextLine();

                 if(userAns.equalsIgnoreCase(plantList.get(i).getCommonName())) //PROBLEM CODE
                    {
                        //Ask what the user wishes to edit.
                        System.out.println("What do you wish to edit?\n1. Common Name."
                                + "\n2. Scientific Name.\n3. Maximum Height.\n4. Price"
                                + "\n5. Is it fragile (true or false)?\n6. Quit.");
                        //Get user choice.
                        choice = scan.nextInt();
                        //Choice 1
                        if(choice == 1)
                        {
                            System.out.println("What is the new Common Name? ");
                            String newComName = scan.nextLine();
                            plantList.get(i).setCommonName(newComName);
                        }
                        //Choice 2
                        if(choice == 2)
                        {
                            System.out.println("What is the new Scientific Name? ");
                            String newSciName = scan.nextLine();
                            plantList.get(i).setScientificName(newSciName);
                        }
                        //Choice 3
                        if(choice == 3)
                        {
                            System.out.println("What is the new Maximum Height? ");
                            double newHeight = scan.nextDouble();
                            plantList.get(i).setMaxHeight(newHeight);
                        }
                        //Choice 4
                        if(choice == 4)
                        {
                            System.out.println("What is the new Price?");
                            double newPrice = scan.nextDouble();
                            plantList.get(i).setPrice(newPrice);
                        }
                        //Choice 5
                        if(choice == 5)
                        {
                            System.out.println("Is the plant Fragile (true or false)? ");
                            boolean newFragile = scan.nextBoolean();
                            plantList.get(i).setFragile(newFragile);
                        }
                        //Choice 6
                        if(choice == 6)
                        {
                            break;
                        }
                    }
                }  
            }
            //Choice 4. End program.
            if(ans == 4)
            {
                break;
            }
        }
    } 
}

Okay so I changed the third choice into a switch statement. Now the problem I have is I can only do one edit. After that first edit when I try to pick another plant or the same one to edit it doesn't read it and keeps asking me the same question.

Code:

//Choice 3. Edit information on plant(s) in ArrayList.
            if(ans == 3)
            {
                OUTER:
                while (true) {
                    int i;
                    String userAns;
                    System.out.println("Which plant to wish to edit (choose the common name)?");
                    for(i = 0; i < plantList.size(); i++)
                    {
                        System.out.println(plantList.get(i));
                    }
                    blankLine = scan.nextLine();
                    userAns = scan.nextLine();
                    if (userAns.equalsIgnoreCase(plantList.get(i-1).getCommonName())) {
                        System.out.println("What do you wish to edit?\n1. Common Name."
                                + "\n2. Scientific Name.\n3. Maximum Height.\n4. Price"
                                + "\n5. Is it fragile (true or false)?\n6. Quit.");
                        choice = scan.nextInt();
                        //Choices
                        switch (choice) 
                        {
                            //Choice 1
                            case 1:
                                System.out.println("What is the new Common Name? ");
                                blankLine = scan.nextLine();
                                String newComName = scan.nextLine();
                                plantList.get(i-1).setCommonName(newComName);
                                break;
                            //Choice 2
                            case 2:
                                System.out.println("What is the new Scientific Name? ");
                                blankLine = scan.nextLine();
                                String newSciName = scan.nextLine();
                                plantList.get(i-1).setScientificName(newSciName);
                                break;
                            //Choice 3
                            case 3:
                                System.out.println("What is the new Maximum Height? ");
                                double newHeight = scan.nextDouble();
                                plantList.get(i-1).setMaxHeight(newHeight);
                                break;
                            //Choice 4
                            case 4:
                                System.out.println("What is the new Price?");
                                double newPrice = scan.nextDouble();
                                plantList.get(i-1).setPrice(newPrice);
                                break;
                            //Choice 5
                            case 5:
                                System.out.println("Is the plant Fragile (true or false)? ");
                                boolean newFragile = scan.nextBoolean();
                                plantList.get(i-1).setFragile(newFragile);
                                break;
                            //Choice 6
                            case 6:
                                break OUTER;
                            default:
                                break;
                        }
                    }
                }  
            }

3 Answers 3

1

ArrayList index starts with 0, so you have to do plantList.get(i-1).setPrice(newPrice);

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

Comments

1

You are starting index 0. So if you type 2 it means you trying to access 3 values array[0], array[1], array[2].

Change for(i = 0; i < plantList.size(); i++) to for(i = 1; i < plantList.size(); i++)

if you do not wish to change your for loop then you need to change plantList.get(i) to plantList.get(i-1) to make sure it is within the range

Comments

1
for(i = 0; i < plantList.size(); i++)
{
    System.out.println(plantList.get(i));
}   

You've incremented i to plantList.size(). When you access the list with plantList.get(i).getCommonName(), i is already larger that the largest index.

You probably shouldn't use a variable defined outside the loop as the counter in the loop.

Have you considered a Map instead of seaching through the list?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.