I've tried many different ways to fix the error. I know it has something to do with the hasNext, I'm just not sure where the error is. Please help. Also if someone could tell me how I can get the value of the variable largestQuantity into the main method from my getLargestQuantityItem method I'd appreciate that. Cheers!
Here is the input file that is a .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
Here is my code:
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);
}
}
Here is the output and the error I'm receiving:
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)