Search code examples
javablock

Java code hangs at Scanner hasNextLine


First off, I'm new to java and trying to complete an assignment from school on creating a vending machine. My program is taking in 2 files as cli arguments, one for products, and the other for money.

For the life of me I cannot figure out why the code is hanging on line 42

 (while (moneyTemp.hasNextLine());)

I tried to debug on eclipse using breakpoints and noticed the code never goes past this line. Putting a print statement inside the while loop, i don't get the output so i know it is not looping.

The java docs say hasNextLine can block waiting for user input, but since my source is a file, I'm not sure why this is happening. See relevant code below.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class VendingMachine
{
    static Scanner input = new Scanner (System.in);

    public static void main(String[] args) 
    {
        try
        {
            Scanner productTempFile = new Scanner(new File(args[0]));
            Scanner moneyTemp = new Scanner(new File(args[1]));
            int numProducts = 0; //Number of products to be loaded to the machines
            int numMoney = 0;   //Number of money objects to be loaded in the machine

            while (productTempFile.hasNextLine()) //This block will get the number of products
            {
                numProducts++;
                productTempFile.nextLine();
            }
            productTempFile.close();


            Product[] invArray = new Product[numProducts];
            Scanner myFile = new Scanner(new File(args[0]));

            for(int i = 0; i < numProducts; i++)  //This block populates the array of products
            {
                String inputLine = myFile.nextLine();                               
                String[] lineArray = inputLine.split(",");

                invArray[i] = new Product(lineArray[0], Double.valueOf(lineArray[1]), lineArray[2], lineArray[3],
                        Double.valueOf(lineArray[4]), Integer.valueOf(lineArray[5]));
            } 

            myFile.close();

            System.out.println("I'm here");

            while (moneyTemp.hasNextLine()); //This block gets the number of different money items
            {
                numMoney++;
                moneyTemp.nextLine();               
            }

Below is the second file i am supplying ie arg[1] which is formatted same like first one which works.

PaperCurrency,100 Dollar Bill,100.0,medium,paper,0
PaperCurrency,50 Dollar Bill,50.0,medium,paper,0
PaperCurrency,20 Dollar Bill,20.0,medium,paper,0
PaperCurrency,10 Dollar Bill,10.0,medium,paper,4
PaperCurrency,5 Dollar Bill,5.0,medium,paper,8
PaperCurrency,1 Dollar Bill,100.0,medium,paper,16
CoinCurrency,50 Cent Piece,0.5,large,metal,10
CoinCurrency,Quarter,0.25,medium,metal,20
CoinCurrency,Dime,0.1,small,metal,30
CoinCurrency,Nickel,0.05,small,metal,40
CoinCurrency,Penny,0.01,small,metal,50

Any help will be very appreciated. Thanks


Solution

  • Remove semicolon from the line

     while (moneyTemp.hasNextLine());
    

    Semicolom make the while loop complete its body without doing anything means its like while(){} when while condition is true do nothing and since your condition is hasNextLine() it checks for same line again and again causing infinite loop.