简体   繁体   English

Java代码挂在Scanner hasNextLine

[英]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. 首先,我是java的新手,正在尝试完成学校创建自动售货机的作业。 My program is taking in 2 files as cli arguments, one for products, and the other for money. 我的程序将2个文件作为cli参数,一个用于产品,另一个用于金钱。

For the life of me I cannot figure out why the code is hanging on line 42 为了我的一生,我无法弄清楚代码为什么挂在第42行上

 (while (moneyTemp.hasNextLine());)

I tried to debug on eclipse using breakpoints and noticed the code never goes past this line. 我尝试使用断点在eclipse上进行调试,并发现代码永远不会超过这一行。 Putting a print statement inside the while loop, i don't get the output so i know it is not looping. 将print语句放入while循环内,我没有得到输出,所以我知道它没有循环。

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. Java文档说hasNextLine可以阻止等待用户输入,但是由于我的源文件是文件,所以我不确定为什么会这样。 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. 以下是我提供的第二个文件,即arg [1],其格式与第一个起作用的格式相同。

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 谢谢

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. Semicolom使while循环完成它的身体没有做任何事情意味着它像while(){}当while条件是真什么也不做,因为你的条件是hasNextLine()它一次又一次地检查同一线路造成无限循环。

By adding the semicolon ( ; ), you are implicitly having the while loop execute an empty block. 通过添加分号( ; ),可以隐式地让while循环执行一个空块。 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. hasNextLine()不会更改扫描程序所基于的InputStream ,因此,由于while循环的主体中没有任何内容,因此也没有任何可更改状态的内容,并且该循环将永远持续下去。

Just drop the semicolon from the while loop, and you should be fine: 只需从while循环中删除分号,就可以了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM