Question: Hey folks, I ' m back with another question. I have a lab activity for school working with arrays. The error I ' m getting
Hey folks, Im back with another question. I have a lab activity for school working with arrays. The error Im getting is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index out of bounds for length at PlantArrayListExample.mainPlantArrayListExamplejava:
There are two other files in this project, a plant file and a flower file, so I could post the code from those if needed but it looks like the issue is in this one specifically.
The goal is if the input is:
plant Spirea
flower Hydrangea false lilac
flower Rose false white
plant Mint
The output will be: the printing formatting has been done on the other two files, flower.java and plant.java
Plant Information:
Plant name: Spirea
Cost: Plant Information:
Plant name: Hydrangea
Cost:
Annual: false
Color of flowers: lilac
Plant Information:
Plant name: Rose
Cost:
Annual: false
Color of flowers: white
Plant Information:
Plant name: Mint
Cost:
Any ideas on where I went wrong with this code? I searched Google and have revisedchanged my code based off of other's solutions and answers multiple times now but I've gotten errors every time and this seems to be the closest I've gotten to results.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class PlantArrayListExample
method to print an ArrayList of plant or flower objects
public static void PrintArrayListArrayList myGarden
forint i; i myGarden new ArrayList;
Declaring variables plantName, plantCost, colorOfFlowers, isAnnual
String plantName;
Double plantCost;
String colorOfFlowers;
Boolean isAnnual;
input scnrnext;
whileinput.equals
import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class PlantArrayListExample
method to print an ArrayList of plant or flower objects
public static void PrintArrayListArrayList myGarden
forint i; i myGarden new ArrayList;
Declaring variables plantName, plantCost, colorOfFlowers, isAnnual
String plantName;
Double plantCost;
String colorOfflowers;
Boolean isAnnual;
input scnrnext;
whileinput.equals
String info input.split; splitting input
ifinfotoLowerCaseequalsplant check if plant or flower and add to array
Plant pObj new Plant;
pObj.setPlantNameinfo;
pObj.setPlantCostinfo;
myGarden.addpObj;
else
Flower fObj new Flower;
fObj.setPlantNameinfo;
fObj.setPlantCostinfo;
fObj.setColorOfFlowersinfo;
fObj.setPlantTypeBooleanparseBooleaninfo;
myGarden.addfObj;
input scnrnext;
Call the method printArrayList to print myGarden
PrintArrayListmyGarden;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
