1. Display one product
2. Display all products
3. Add a new CD
6. Exit
Enter your choice: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at ProgTwo.main(ProgTwo.java:31)
What exactly does "Exception in thread "main" java.util.NoSuchElementException" mean and how can I fix it so my program runs properly?
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.text.NumberFormat;
- public class Product
- {
- private String name;
- private double price;
- private int idNum;
- private int quantity;
- public Product (int id, String title, double cost, int total)
- {
- id = idNum;
- name = title;
- price = cost;
- quantity = total;
- }
- //-----------------------------------------------------------------
- // Returns the product's ID number.
- //-----------------------------------------------------------------
- public int getID()
- {
- return idNum;
- }
- //-----------------------------------------------------------------
- // Returns the product's name.
- //-----------------------------------------------------------------
- public String getName()
- {
- return name;
- }
- //-----------------------------------------------------------------
- // Returns the product's price.
- //-----------------------------------------------------------------
- public double getPrice()
- {
- return price;
- }
- //-----------------------------------------------------------------
- // Returns the quantity in inventory.
- //-----------------------------------------------------------------
- public int getQuantity()
- {
- return quantity;
- }
- //-----------------------------------------------------------------
- // Returns a printablle version of the product object.
- //-----------------------------------------------------------------
- public String toString()
- {
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- String description;
- description = fmt.format(price) + "\t" + quantity + "\t";
- description += idNum + "\t" + name;
- return description;
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.text.NumberFormat;
- public class ProductCollection
- {
- private Product[] collection;
- private int count;
- private double totalCost;
- //-----------------------------------------------------------------
- // Constructor: Creates an initially empty collection.
- //-----------------------------------------------------------------
- public ProductCollection ()
- {
- collection = new Product[100];
- count = 0;
- totalCost = 0.0;
- }
- //-----------------------------------------------------------------
- // Adds a product to the collection.
- //-----------------------------------------------------------------
- public void addProduct (int prodID, String prodName, double unitPrice, int prodQty)
- {
- if (count == collection.length)
- increaseSize();
- int index = findProduct(prodID);
- if (index == -1)
- {
- collection[count] = new Product (prodID, prodName, unitPrice, prodQty);
- totalCost += unitPrice;
- count++;
- }
- else
- System.out.println("Product was not added, a product having the ID number " + prodID
- + " was found.");
- }
- //-----------------------------------------------------------------
- // Increases the size of the product to the collection.
- //-----------------------------------------------------------------
- private void increaseSize ()
- {
- Product[] temp = new Product[collection.length * 2];
- for (int prod = 0; prod < collection.length; prod++)
- temp[prod] = collection[prod];
- collection = temp;
- }
- //-----------------------------------------------------------------
- // Finds product from collection.
- //-----------------------------------------------------------------
- public int findProduct(int prodID)
- {
- int index = -1;
- for (int i = 0; i < count; i++)
- if (collection[i].getID() == prodID)
- index = i;
- return index;
- }
- //-----------------------------------------------------------------
- // Deletes product from the collection.
- //-----------------------------------------------------------------
- public void deleteProduct(int prodID)
- {
- int index = findProduct(prodID);
- if (index > -1)
- {
- for (int i = index; i < count; i++)
- {
- collection[i] = collection[i+1];
- count--;
- }
- }
- else System.out.println("A product having the ID number " + prodID
- + " was not found.");
- }
- //-----------------------------------------------------------------
- // Displays product from the collection.
- //-----------------------------------------------------------------
- public void displayProduct(int prodID)
- {
- int index = findProduct(prodID);
- if (index > -1)
- System.out.println(collection[index]);
- else
- System.out.println("A product having the ID number " + prodID
- + " was not found.");
- }
- //-----------------------------------------------------------------
- // Returns a report describing the Product collection.
- //-----------------------------------------------------------------
- public String createOutputFile()
- {
- String data = "";
- for (int i = 0; i < count; i++)
- {
- data += collection[i].getID() + ",";
- data += collection[i].getName() + ",";
- data += collection[i].getPrice() + ",";
- data += collection[i].getQuantity() + ",\n";
- }
- return data;
- }
- public String toString()
- {
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- String report = "-------------------------------------------\n";
- report += "My Product Collection\n\n";
- report += "Number of products: " + count + "\n";
- report += "Total cost: " + fmt.format(totalCost) + "\n";
- report += "Average cost: " + fmt.format(totalCost/count);
- report += "\n\nCD List:\n\n";
- for (int cd = 0; cd < count; cd++)
- report += collection[cd].toString() + "\n";
- return report;
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- import java.io.*;
- public class ProgTwo
- {
- public static void main (String[] args) throws FileNotFoundException, IOException
- {
- Scanner scan = new Scanner(new File("productin.txt"));
- ProductCollection data = new ProductCollection ();
- String name = null;
- double price = 0.0;
- int idNum = 0, quantity = 0, input = 0;
- while (scan.hasNextLine())
- {
- scan.useDelimiter(",");
- idNum = scan.nextInt();
- name = scan.next();
- price = scan.nextDouble();
- quantity = scan.nextInt();
- scan.nextLine();
- data.addProduct (idNum, name, price, quantity);
- }
- do
- {
- System.out.println("1. Display one product");
- System.out.println("2. Display all products");
- System.out.println("3. Add a new product");
- System.out.println("6. Exit");
- System.out.print("\nEnter your choice: ");
- input = scan.nextInt();
- switch (input)
- {
- case 1: System.out.print("Enter the ID number of the product to display: " );
- idNum = scan.nextInt();
- data.displayProduct(idNum);
- break;
- case 2: System.out.println (data);
- break;
- case 3: System.out.print("Enter the ID number: ");
- idNum =scan.nextInt();
- System.out.print("Enter the name: " );
- name = scan.nextLine();
- System.out.print("Enter the price: " );
- price = scan.nextDouble();
- System.out.print("Enter the quantity: ");
- quantity = scan.nextInt();
- data.addProduct(idNum, name, price, quantity);
- System.out.println(data);
- break;
- }
- } while (input != 6);
- FileWriter fw = new FileWriter("productout.txt");
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter pw = new PrintWriter(bw);
- pw.println(data.createOutputFile());
- pw.close();
- }
- }