My code is not working and I have no idea why. This is the error I'm receiving:
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)
I need to take 2 arrayLists (Quantity, Prices) and multiply the values of the 2 and store them in a new arrayList and then find the min and max of that arrayList.
My code:
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);
}
}
My output:
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)
Input File (.txt file):
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