如何修复NoSuchElementException?

时间:2015-01-20 04:50:52

标签: java exception-handling

我尝试了很多不同的方法来修复错误。我知道它与hasNext有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从我的getLargestQuantityItem方法中将变量maximumQuantity的值输入main方法,我会很感激。干杯!

这是一个.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

这是我的代码:

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

public class Project01 {

public static void main(String[] args) {
    ArrayList<String> Titles = new ArrayList<String>();
    ArrayList<String> Types = new ArrayList<String>();
    ArrayList<Double> Prices = new ArrayList<Double>();
    ArrayList<Integer> Quantities = new ArrayList<Integer>();
    int count = 0;
    Scanner in = new Scanner(System.in);
    String database = getFile(in);
    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.hasNext()) {
            getTitle(Titles, inputFile.nextLine());
            getQuantity(Quantities, inputFile.nextInt());
            getPrice(Prices, inputFile.nextDouble());
            getType(Types, inputFile.next());
            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++;
            inputFile.next();
        }
        System.out.println("-----------------------------------------------------------------");
        System.out.println("Total products in database: " + count);
        System.out.println("Largest quantity item: " + largestQuantity);
        System.out.println("Highest total dollar item: ");
        System.out.println("Smallest quantity item: ");
        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) {
    Titles.add(title);
}
private static void getType(ArrayList<String> Types, String type) {
    Types.add(type);
}
private static void getPrice(ArrayList<Double> Prices, double price) {
    Prices.add(price);
}
private static void getQuantity(ArrayList<Integer> Quantities, int quantity) {
    Quantities.add(quantity);
}
private static void getLargestQuantityItem(ArrayList<Integer> Quantities){
    Integer largestQuantity = Collections.max(Quantities);
    }
}

以下是我收到的输出和错误:

Enter database filename: 

Product Summary Report

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

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

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

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

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

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

java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at Project01.main(Project01.java:31)

1 个答案:

答案 0 :(得分:2)

参考this帖子和你的txt文件。

inputFile.nextLine()nextInt()方法后添加nextDouble()

getQuantity(Quantities, inputFile.nextInt());
inputFile.nextLine();
getPrice(Prices, inputFile.nextDouble());
inputFile.nextLine();

更改以下代码

getType(Types, inputFile.nextLine());

并在while循环结束时移除inputFile.next();

第二种方法只会在next();方法调用周围添加阻止。

if (inputFile.hasNext()) {
    inputFile.next();
}

问题是while循环结束时next();正在读取下一个元素。但是在Book之后的文件末尾没有任何内容可读,因此它在该行中抛出异常。

<强>更新

largestQuantity是一个局部变量,无法在方法外访问。您应该从getLargestQuantityItem方法返回它。因此,更改方法的返回类型,然后返回最大值。

private static Integer getLargestQuantityItem(ArrayList<Integer> Quantities){
    return Collections.max(Quantities);
}

主要方法

获取最大数量项目:

System.out.println("Lowest total dollar item: ");
Integer largestQuantityMainVariable = getLargestQuantityItem(Quantities);
System.out.println("Largest quantity : " + largestQuantityMainVariable);

int index;
for (int i = 0; i < Quantities.size(); i++) {
    if (Quantities.get(i) != null && Quantities.get(i).equals(largestQuantityMainVariable)) {
        index = i;
        break;
    }
}

System.out.println("Largest quantity item : " + Titles.get(index));

更新2:

对于评论The only thing I have left is to find the highest and lowest total dollar value. I need to somehow take the quantity times the price and then look for the max and min value out of those values

中的问题
private static List<Double> generateTotalPriceList(List<Double> prices, List<Integer> quantities) {
    List<Double> totalPriceList = null;
    if (prices != null && prices.size() > 0
            && quantities != null && quantities.size() > 0) {
        if (prices.size() == quantities.size()) {
            totalPriceList = new ArrayList<Double>();
            double totalValue;
            for (int i = 0; i < prices.size(); i++) {
                totalValue = prices.get(i) * (double) quantities.get(i);
                totalPriceList.add(totalValue);
            }
        }
    }
    return totalPriceList;
}

主要方法

List<Double> totalPriceList = generateTotalPriceList(Prices, Quantities);

现在,您可以按照我们对Quantity所做的步骤进行操作。