0

This is my code for printing a shopping list. It works fine when I put the data manually at runtime through keyboard. But when I direct an input.txt file as an argument in command line it throws NoSuchElementException after 1st iteration.

I think it might have to do with the sequence of using Scanner.next() and Scanner.nextInt() but not sure.

How do I fix this?

import java.io.*;
import java.util.*;
class Product
{
    Scanner sc = new Scanner(System.in);
    int productID;
    String productName;
    String productType;
    float productPrice;

    Product()
    {
        //  Do Nothing.
    }

    void createProduct()
    {
        System.out.println("Enter the Product ID: ");
        productID = sc.nextInt();
        System.out.println("Enter the Product Name: ");
        productName = sc.next();
        System.out.println("Enter the Product Type: ");
        productType = sc.next();
        System.out.println("Enter the Product Price: ");
        productPrice = sc.nextFloat();
    }

    Product(int pID, String pName, String pType, float pPrice)
    {
        productID = pID;
        productName = pName;
        productType = pType;
        productPrice = pPrice;
    }

    void showIdentification()
    {
        System.out.println(productID + " " + productName);
    }

    void showDetail()
    {
        System.out.println(productID + " " + productName + " " +  productType + " " + productPrice);    
    }
}

class Shop
{
    int n = 10;
    Product list[] = new Product[n];


    void createList()
    {
        for(int i=0; i<n; i++)
        {
            System.out.println("Product No. " + (i+1));
            Product o = new Product();
            o.createProduct();
            o.showDetail();
            list[i] = o;
        }
    }

    void viewAllProducts()
    {
        for(int i=0; i<n; i++)
        {
            list[i].showIdentification();
        }
    }
}

class ShopList
{

    public static void main(String[] args)
    {
        Shop sList = new Shop();
        sList.createList();
        sList.viewAllProducts();
    }
}

Here is the input file :-

1001 CadburyA ChocolateA 15.00
1002 CadburyB ChocolateB 16.00
1003 CadburyC ChocolateC 17.00
1004 CadburyD ChocolateD 18.00
1005 CadburyE ChocolateE 19.00
1006 CadburyF ChocolateF 20.00
1007 CadburyG ChocolateG 21.00
1008 CadburyH ChocolateH 22.00
1009 CadburyI ChocolateI 23.00
1010 CadburyJ ChocolateJ 24.00

and here is the exception I got:-

java ShopList < input.txt
Product No. 1
Enter the Product ID: 
Enter the Product Name: 
Enter the Product Type: 
Enter the Product Price: 
1001 CadburyA Chocolate 15.0
Product No. 2
Enter the Product ID: 
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Product.createProduct(ShopList.java:19)
    at Shop.createList(ShopList.java:59)
    at ShopList.main(ShopList.java:95)
Stolaine
  • 25
  • 4
  • 1
    @AlexR Of course not. – user202729 Apr 01 '18 at 02:00
  • @Logan - it didn't improve the situation. Elaborate – Stolaine Apr 01 '18 at 02:00
  • 1
    [mcve] please. Any reason you included `viewProductDetail` method while you don't even use them? – user202729 Apr 01 '18 at 02:00
  • @user202729 viewProductDetail is to be used further to view a particular products detail based on its ID, I am stuck with creating the list. – Stolaine Apr 01 '18 at 02:04
  • @Stolaine It's unrelated to your **current** problem (NoSuchElement exception). **Don't** include it. Please read the [mcve] page for more details. – user202729 Apr 01 '18 at 02:06
  • [Possible duplicate](https://stackoverflow.com/questions/28249102/what-is-java-util-nosuchelementexception-and-how-do-i-fix-it?noredirect=1&lq=1). [Possible duplicate](https://stackoverflow.com/questions/4232588/how-to-use-multiple-scanner-objects-on-system-in). – user202729 Apr 01 '18 at 02:09
  • 1
    @user202729: You're right in that the code is not minimal, but it is at least cve and contains the necessary testdata - it is close to optimal, compared with the crystal-ball-code, we see so often. :) Even program invocation with complete error output with accurate line numbers. How often do we find that? – user unknown Apr 01 '18 at 02:29
  • @userunknown "at least". – user202729 Apr 01 '18 at 02:30
  • 1
    @user202729: And proper indentation! With 1 reputation point. Close to excellent. – user unknown Apr 01 '18 at 02:32
  • Suggestion: A minimal example would look like [this](https://tio.run/##fVJBa8IwGL33V3x4SlcW9Nx5GwNhiNDdxEMWo36uTUqSusnwt3dJ2tpaZaG08N7L1/decmQn9qxKIY/br7rGolTawtGBFBV9SqMhUlnMPcZzZgystNpW3Ea/EbiVcSal0GA4zEGK7w4g2dlYUVCUcRqEKC2UzdbFawNlVqPcd@iSFeIR/nEuW3yXK3YdstLIHR6Ik8ItcC2YFa07Egei8ejX9dfOpuFUih@7kE6WjhXeRq95IPB@/hUEZ73izbvuZJcock9TZHZQZduiL0e6LbNpI2tTQI7Grjdtsy24lpv72O9OeJd5pzTxk3E@TQFfpHslSXyle2EovTkvVVlauvptLsmks7FUFCaQAMFkFg8CD62qW5dkJFN0dD63dAiKPqnqicvDynzUtray@syRg7HMuk@oo2AoSXOBXHFM7824FT8DjJ/S3VgHDP0Ejg577c7uEtX1Hw). – user202729 Apr 01 '18 at 02:35

1 Answers1

0

You have to make Scanner static:

class Product
{
    // There is only one System.in - the Scanner can be shared between 
    // all instances
    static Scanner sc = new Scanner(System.in);
    int productID;
    String productName;
    String productType;
    float productPrice;

    Product ()
    {
        //  Do Nothing.
    }

    void createProduct()
    {
        System.out.println(" Enter the Product ID: ");
        productID = sc.nextInt ();
        // for debugging, I inserted the last value in the printlns:
        System.out.println (productID + " Enter the Product Name: ");
        productName = sc.next ();
        System.out.println (productName + " Enter the Product Type: ");
        productType = sc.next ();
        System.out.println (productType + " Enter the Product Price: ");
        productPrice = sc.nextFloat ();
        System.out.println (productPrice + " ");
        /* Helpless attemps to find out, if we need to consume the line break :)
        String s = "dummy";
        while (s != "\n") {
            s = sc.next ();
            System.out.println ("<" + s + ">");
        }
        */
    }

No other modifications were made.

Well, not exactly. As continental European, I had to tell the system, that my numbers are dot delimited or better, pass in comma delimited ones.

cat choco.lst | sed 's/\./,/' | java ShopList

That might not be the case on your system.

Well, and the Scanner in ShopList, which is not used, shall vanish too.

user unknown
  • 35,537
  • 11
  • 75
  • 121