如何修复我的 NoSuchElementException?
How can I fix my NoSuchElementException?
我尝试了很多不同的方法来修复错误。我知道它与hasNext有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从我的 getLargestQuantityItem 方法中将变量 largestQuantity 的值获取到 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)
参考 this post 和您的 txt 文件。
在您的 nextInt()
、nextDouble()
方法之后添加 inputFile.nextLine()
。
getQuantity(Quantities, inputFile.nextInt());
inputFile.nextLine();
getPrice(Prices, inputFile.nextDouble());
inputFile.nextLine();
更改以下代码
getType(Types, inputFile.nextLine());
并在 while 循环结束时删除 inputFile.next();
。
第二种方法 将仅添加 if 块围绕您的 next();
方法调用。
if (inputFile.hasNext()) {
inputFile.next();
}
问题是 while 循环末尾的 next();
正在读取下一个元素。但是在 Book
之后的文件末尾没有任何内容可读,因此它在该行抛出异常。
更新:
largestQuantity
是局部变量,方法外不能访问。你应该 return 它来自 getLargestQuantityItem
方法。因此,更改方法的 return 类型,然后 return 最大值。
private static Integer getLargestQuantityItem(ArrayList<Integer> Quantities){
return Collections.max(Quantities);
}
并且在 main 方法中
获得最大数量的项目:
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
所做的步骤进行操作。
我尝试了很多不同的方法来修复错误。我知道它与hasNext有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从我的 getLargestQuantityItem 方法中将变量 largestQuantity 的值获取到 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)
参考 this post 和您的 txt 文件。
在您的 nextInt()
、nextDouble()
方法之后添加 inputFile.nextLine()
。
getQuantity(Quantities, inputFile.nextInt());
inputFile.nextLine();
getPrice(Prices, inputFile.nextDouble());
inputFile.nextLine();
更改以下代码
getType(Types, inputFile.nextLine());
并在 while 循环结束时删除 inputFile.next();
。
第二种方法 将仅添加 if 块围绕您的 next();
方法调用。
if (inputFile.hasNext()) {
inputFile.next();
}
问题是 while 循环末尾的 next();
正在读取下一个元素。但是在 Book
之后的文件末尾没有任何内容可读,因此它在该行抛出异常。
更新:
largestQuantity
是局部变量,方法外不能访问。你应该 return 它来自 getLargestQuantityItem
方法。因此,更改方法的 return 类型,然后 return 最大值。
private static Integer getLargestQuantityItem(ArrayList<Integer> Quantities){
return Collections.max(Quantities);
}
并且在 main 方法中
获得最大数量的项目:
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
所做的步骤进行操作。