我正在研究一個有兩個類的植物苗圃的程序; PlantNursery和植物。用戶獲得提升4個問題。 1)添加植物,2)列出所有植物,3)編輯植物,4)退出。我有1,2和4工作正常。我的問題在於選項3中。我在數組中顯示當前植物列表,並要求用戶選擇一個通用名稱。然後我存儲該字符串並將其與if語句進行比較。 if
聲明未按預期工作。我得到的錯誤信息:閱讀用戶輸入爲一個字符串,並將其與任何ArrayList進行比較以進行驗證
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)
植物類:
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類:
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;
}
}
}
}
好了,所以我改變了第三種選擇到一個switch語句。現在我遇到的問題是我只能做一個編輯。在第一次編輯之後,當我嘗試挑選另一個植物或編輯它時,它不會讀取它並不斷詢問我同一個問題。
代碼:
//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;
}
}
}
}