我正在尝试将条目添加到Java中的ArrayList中,并且我已将其工作,但现在却没有。这是代码
import java.util.*;
public class SalesPerson
{
// define an array of allowable sales person to be three
static String[] salesPerson = new String[3];
/**
* Calculates the total annual compensation for a salesperson by
* multiplying annual sales by commission rate and then adding the salary
*/
public static void compensation()
{
try {
String[] salesperson = new String[3];
// use scanner class to read input from the user
Scanner input = new Scanner(System.in);
// List to add entries to an array list
ArrayList<String> list = new ArrayList<>();
// do..while loop to add salespersons
do {
System.out.println("Want to compare additional " //GLD-changed verbiage here
+ "salespeople? (y/n)"); //GLD-changed verbiage here
if (input.next().startsWith("y")) {
System.out.println("Enter the salesperson's name:");
addSalesPerson(list).add(input.next());
System.out.println("Enter annual amount for salesperson");
addSalesPerson(list).add(input.next());
} else {
break;
}
}
while (true);
// System.out.println(addSalesPerson(list).get(1));
// check to see if only two sales persons have been entered
if (addSalesPerson(list).size() < 2 || addSalesPerson(list).size() > 4) {
throw new Exception("You may only enter two sales persons.");
}
Object[] arr;
arr = addSalesPerson(list).toArray(new String[addSalesPerson(list).size()]);
// loop through the length of the array
for (Object arr1 : arr) {
salesperson = (String[]) arr;
}
// for this, we will compare two sales people only
float sale2 = Float.parseFloat(salesperson[1]);
float sale4 = Float.parseFloat(salesperson[3]);
// subtract based on which variable is greater to ensure no negative values
float difference = sale2 > sale4 ? sale2 - sale4 : sale4 - sale2;
// print out the difference
System.out.printf("To match the other sales person's amount "
+ "you will need to get $%.2f more\n\n", difference);
} catch (Exception e) {
// handle any exception by displaying an error message
System.out.println("Something went wrong while processing your "
+ "input. Please be sure you only entered numeric values"
+ " and or the correct amount of salespersons.");
}
}
/**
* Adds sales people to an array list
* @param salesperson
* @return ArrayList
*/
static public ArrayList addSalesPerson(ArrayList<String> salesperson)
{
for (int i=0; i<salesPerson.length; i++) {
if (salesperson.get(i).equals("")) {
break;
}
salesperson.add(salesperson.get(i));
}
return salesperson;
}
}
然后我在另外两个文件中调用这些静态方法
public class Getters
{
public static void handleSalesPersonCompensation()
{
SalesPerson.compensation();
}
}
public class ComparisonWk5
{
public static void main(String args[])
{
Getters.handleSalesPersonCompensation();
}
}
这些的输出应该是 想要比较其他销售人员? (Y / N) ÿ 输入销售人员的姓名:我的姓名 输入销售员的年度金额:200000 想要比较其他销售人员? (Y / N) ÿ 输入销售人员的姓名:我的姓名2 输入销售员的年度金额:100000
要匹配其他人的金额,您需要再获得100000美元
但输出是: 想要比较其他销售人员? (Y / N) ÿ 输入销售人员的姓名: 处理输入时出错了。请确保您只输入了数值和/或正确数量的销售人员。
每次输入“y”后,它都会显示销售人员的姓名输入,但也会在没有输入名称的情况下立即显示例外。
任何帮助将不胜感激
堆栈跟踪是这样的:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at SalesPerson.addSalesPerson(SalesPerson.java:96)
at SalesPerson.compensation(SalesPerson.java:48)
at Getters.handleSalesPersonCompensation(Getters.java:61)
at ComparisonWk5.main(ComparisonWk5.java:31)
由于