尝试获取2个arrayLists并将它们相乘并将新值存储到新的arrayList中以查找min / max

时间:2015-01-20 22:02:39

标签: java arraylist max min

我的代码无效,我不知道为什么。这是我收到的错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:350)
at java.util.Collections.max(Collections.java:638)
at Project01.getHighestDollarAmount(Project01.java:120)
at Project01.main(Project01.java:45)

我需要获取2个数组列表(数量,价格)并将2的值相乘并将它们存储在新的arrayList中,然后找到该arrayList的最小值和最大值。

我的代码:

import java.util.*;
import java.io.*;

public class Project01 {

public static void main(String[] args) {
    ArrayList<String> Titles = new ArrayList<String>();//Declare the array lists that will be used.
    ArrayList<String> Types = new ArrayList<String>();
    ArrayList<Double> Prices = new ArrayList<Double>();
    ArrayList<Integer> Quantities = new ArrayList<Integer>();
    ArrayList<Double> Dollars = new ArrayList<Double>();
    int count = 0;//Set the counter to zero.
    Scanner in = new Scanner(System.in);//Establish the scanner so user input can be properly read.
    String database = getFile(in);//Setting the file name variable from the method below that asks the user for the file's name.
    try {
        File file = new File(database);
        Scanner inputFile = new Scanner(file);
        System.out.println();
        System.out.println("Product Summary Report");
        System.out.println("------------------------------------------------------------");
        while (inputFile.hasNextLine()) {
            getTitle(Titles, inputFile.nextLine());
            getQuantity(Quantities, inputFile.nextInt());
            inputFile.nextLine();
            getPrice(Prices, inputFile.nextDouble());
            inputFile.nextLine();
            getType(Types, inputFile.nextLine());
            System.out.println("Title: " + Titles.get(count));
            System.out.println(" Product Type: " + Types.get(count));
            System.out.println(" Price: " + Prices.get(count));
            System.out.println(" Quantity: " + Quantities.get(count));
            System.out.println();
            count++;
        }
        System.out.println("-----------------------------------------------------------------");
        System.out.println("Total products in database: " + count);
        Integer index = getLargestQuantityTitle(Quantities);
        System.out.println("Largest quantity item : " + Titles.get(index) + " (" + Types.get(index) + ")");
        Double highestTotalDollarAmount = getHighestDollarAmount(Dollars);
        System.out.println("Highest total dollar item: $" + highestTotalDollarAmount);
        Integer index2 = getSmallestQuantityTitle(Quantities);
        System.out.println("Smallest quantity item: " + Titles.get(index2) + " (" + Types.get(index2) + ")");
        System.out.println("Lowest total dollar item: ");
        System.out.println("-----------------------------------------------------------------");
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + database);

    }
    in.close();
}
private static String getFile(Scanner inScanner) {
    System.out.print("Enter database filename: ");
    String fileName = inScanner.nextLine();
    return fileName;
}
private static void getTitle(ArrayList<String> Titles, String title) { //This method is creating the array list of the titles from the input file.
    Titles.add(title);
}
private static void getType(ArrayList<String> Types, String type) { //This method is creating the array list of the types from the input file.
    Types.add(type);
}
private static void getPrice(ArrayList<Double> Prices, double price) { //This method is creating the array list of the prices from the input file.
    Prices.add(price);
}
private static void getQuantity(ArrayList<Integer> Quantities, int quantity) { //This method is creating the array list of the quantities from the input file.
    Quantities.add(quantity);
}
private static Integer getLargestQuantityItem(ArrayList<Integer> Quantities){ //This method is determining the maximum value within the quantities array list.
    return Collections.max(Quantities);
    }
private static Double getHighestPricedItem(ArrayList<Double> prices){ //This method is determining the maximum price within the prices array list.
    return Collections.max(prices);
}
private static Integer getHighestTotalDollarItem(ArrayList<Integer> Prices){ //This method is determining the maximum total value, basically the highest quantity of the item multiplied by it's price.
    return Collections.max(Prices);
}
private static Integer getSmallestQuantityItem(ArrayList<Integer> Quantities){ //This method is determining the minimum value within the quantities array list.
    return Collections.min(Quantities);
    }
private static Integer getLargestQuantityTitle(ArrayList<Integer> Quantities){
    int index = 0;
    Integer largestQuantityMainVariable = getLargestQuantityItem(Quantities);
    for (int i = 0; i < Quantities.size(); i++) {
        if (Quantities.get(i) != null && Quantities.get(i).equals(largestQuantityMainVariable)) {
            index = i;
            break;
        }
    }
    return index;
}
private static Integer getSmallestQuantityTitle(ArrayList<Integer> Quantities){
    int index2 = 0;
    Integer smallestQuantityMainVariable = getSmallestQuantityItem(Quantities);
    for (int i = 0; i < Quantities.size(); i++) {
        if (Quantities.get(i) != null && Quantities.get(i).equals(smallestQuantityMainVariable)) {
            index2 = i;
            break;
        }
    }
    return index2;
}
private static ArrayList<Double> Dollars (ArrayList<Integer> Quantities, ArrayList<Double> Prices){
    int counter=0;
    while (counter<Quantities.size()){
        ArrayList<Double> Dollars = new ArrayList<Double>();
        Dollars.add(Quantities.get(counter)*Prices.get(counter));
    counter++;
    }

    return Dollars(null, null);
}
private static Double getHighestDollarAmount(ArrayList<Double> Dollars){ //This method is determining the maximum price within the prices array list.
    return Collections.max(Dollars);
}

}

我的输出:

Enter database filename: /Desktop/proj1_input

Product Summary Report
------------------------------------------------------------
Title: The Shawshank Redemption
Product Type: DVD
Price: 19.95
Quantity: 100

Title: The Dark Knight
Product Type: DVD
Price: 19.95
Quantity: 50

Title: Casablanca
Product Type: DVD
Price: 9.95
Quantity: 137

Title: The Girl With The Dragon Tattoo
Product Type: Book
Price: 14.95
Quantity: 150

Title: Vertigo
Product Type: DVD
Price: 9.95
Quantity: 55

Title: A Game of Thrones
Product Type: Book
Price: 8.95
Quantity: 100

-----------------------------------------------------------------
Total products in database: 6
Largest quantity item : The Girl With The Dragon Tattoo (Book)
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:350)
at java.util.Collections.max(Collections.java:638)
at Project01.getHighestDollarAmount(Project01.java:120)
at Project01.main(Project01.java:45)

输入文件(.txt文件):

The Shawshank Redemption
100
19.95
DVD
The Dark Knight
50
19.95
DVD
Casablanca
137
9.95
DVD
The Girl With The Dragon Tattoo
150
14.95
Book
Vertigo
55
9.95
DVD
A Game of Thrones
100
8.95
Book

4 个答案:

答案 0 :(得分:3)

我知道你遇到问题的地方。您正在每次迭代时使用Dollars方法初始化ArrayList。 更改此代码

while (counter<Quantities.size()){
        ArrayList<Double> Dollars = new ArrayList<Double>();
        Dollars.add(Quantities.get(counter)*Prices.get(counter));
        counter++;
    }

到此

ArrayList<Double> Dollars = new ArrayList<Double>();

    while (counter<Quantities.size()){
            Dollars.add(Quantities.get(counter)*Prices.get(counter));
        counter++;
        }

所以最终的方法看起来应该是

private static ArrayList<Double> toDollars(ArrayList<Integer> quantities, ArrayList<Double> prices){
int counter=0;
ArrayList<Double> outputInDollars= new ArrayList<Double>();
while (counter<quantities.size()){
    outputInDollars.add(quantities.get(counter)*prices.get(counter));
counter++;
}

return outputInDollars;
}

我还建议您使用for循环,但这取决于您。尝试使用camelcase命名变量

答案 1 :(得分:1)

我没有看到主要调用的美元方法(实际上是数量和价格倍增的方法)。此外,您在while循环中实例化Dollars Arraylist,因此您将丢失对列表中添加的旧数据的引用。如果这就是你想要做的,为什么甚至还有一个计数器?

使用名为Dollars的方法以及具有相同名称的属性会导致很多混淆。请不要这样做。

使用List而不是ArrayList作为引用。仅在实例化对象时使用ArrayList。 实例属性名称应以小写字母或下划线开头。

答案 2 :(得分:0)

如果集合为空,Collections.max()会抛出NoSuchElementException。这就是你的问题。您不要在美元列表中添加任何值。 另外,你永远不会打电话给美元() 还会更新您的美元功能:

private static ArrayList<Double> Dollars (ArrayList<Integer> Quantities, ArrayList<Double> Prices){
    int counter=0;
    // create arrayList outside of the while and return it
    ArrayList<Double> Dollars = new ArrayList<Double>();

    while (counter<Quantities.size()){

        Dollars.add(Quantities.get(counter)*Prices.get(counter));
        counter++;
    }
    // you returned a call to the same function there -> recusive until death
    return Dollars;
}

答案 3 :(得分:0)

该方法导致异常:

private static Double getHighestDollarAmount(ArrayList<Double> Dollars) {
    return Collections.max(Dollars);
}

因为ArrayList Dollars为空。

您可以在此处调用该方法:

Double highestTotalDollarAmount = getHighestDollarAmount(Dollars);

提供的列表Dollars为空,因为它未在此while循环中使用:

while (inputFile.hasNextLine()) {
    getTitle(Titles, inputFile.nextLine());
    getQuantity(Quantities, inputFile.nextInt());
    inputFile.nextLine();
    getPrice(Prices, inputFile.nextDouble());
    inputFile.nextLine();
    getType(Types, inputFile.nextLine());
    System.out.println("Title: " + Titles.get(count));
    System.out.println(" Product Type: " + Types.get(count));
    System.out.println(" Price: " + Prices.get(count));
    System.out.println(" Quantity: " + Quantities.get(count));
    System.out.println();
    count++;
}

因此,您要么混合列表DollarsPrices并想要致电:

Double highestTotalDollarAmount = getHighestDollarAmount(Prices);

或者您忘记在某处“填写”Dollars列表。

修改

我已更新您的Dollars方法:

 private static List<Double> getTotalPrices(List<Integer> quantities, List<Double> prices){
      List<Double> totalPrices = new ArrayList<>();
      for (int i = 0; i < quantities.size(); i++) {
           totalPrices.add(quantities.get(i) * prices.get(i));
      }
      return totalPrices;
 }

我做了以下更改:

  1. 我已将方法重命名为getTotalPrices,因为此名称比Dollars更具描述性,并且跟随Java Naming Conventions(方法名称应以小写字母开头)
  2. 我已将类型从ArrayList更改为List,因为您应始终program to an interface而不是特定类型
  3. 由于命名约定
  4. ,我已更改参数名称
  5. 我用while循环替换了for循环,因为在这种情况下这些更好(迭代列表)
  6. 我已将返回值更改为新列表,而不是递归调用(这会导致更多麻烦)