Java代码在Scanner hasNextLine上挂起 [英] Java code hangs at Scanner hasNextLine

查看:134
本文介绍了Java代码在Scanner hasNextLine上挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是java的新手,并尝试在学校完成创建自动售货机的任务。我的程序将2个文件作为cli参数,一个用于产品,另一个用于金钱。

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.

对于我的生活,我无法弄清楚为什么代码挂在第42行

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

 (while (moneyTemp.hasNextLine());)

我试图调试在使用断点的eclipse上注意到代码永远不会超过这一行。在while循环中放置一个print语句,我没有得到输出,所以我知道它不是循环。

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.

java文档说hasNextLine可以阻止等待用户输入,但由于我的源是一个文件,我不知道为什么会发生这种情况。请参阅下面的相关代码。

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();               
            }

以下是我提供的第二个文件,即arg [1],格式化与第一个有效相同。

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

推荐答案

从行中删除分号

 while (moneyTemp.hasNextLine());

Semicolom使while循环完成其身体而不做任何意味着它像而(){} 当条件为真时什么也不做,因为你的条件是 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.

这篇关于Java代码在Scanner hasNextLine上挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆