Java code hangs at Scanner hasNextLine

2019-02-25 21:02发布

问题:

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

回答1:

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.



回答2:

By adding the semicolon (;), you are implicitly having the while loop execute an empty block. hasNextLine() doesn't doesn't change the InputStream the scanner is based on, so since there's nothing in the while loop's body, there's nothing to change the state, and the loop will just continue forever.

Just drop the semicolon from the while loop, and you should be fine:

while (moneyTemp.hasNextLine()) // no ; here!
{
    numMoney++;
    moneyTemp.nextLine();               
}


标签: java block