0

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)
Seumas Frew
  • 25
  • 2
  • 9

1 Answers1

2

In reference to this post and your txt file.

Add inputFile.nextLine() after your nextInt(), nextDouble() method.

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

Change below code

getType(Types, inputFile.nextLine());

And remove your inputFile.next(); at the end of your while loop.

Second approach will be just add if block around your next(); method call.

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

The problem was that next(); at the end of the while loop was reading the next element. But at the end of file after Book there was nothing to read so it was throwing Exception at that line.

Update:

largestQuantity is a local variable, it can not be accessed outside the method. You should return it from getLargestQuantityItem method. So change the return type of your method and then return the max value.

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

And in main method

To get Largest quantity item:

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));

Update 2:

For the question in the comments 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;
}

In main method

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

Now you can follow the procedure as we did for Quantity.

Community
  • 1
  • 1
Naman Gala
  • 4,480
  • 1
  • 18
  • 48
  • So when dealing with a double list you have to use (double) list.get(i) for the index? That was one of the things giving me trouble. – Seumas Frew Jan 21 '15 at 05:25
  • if you don't put `(double)` then also it is not giving any compilation error. You can check the output with or without (double). – Naman Gala Jan 21 '15 at 05:33