Java code hangs at Scanner hasNextLine

后端 未结 2 1600
轻奢々
轻奢々 2021-01-22 17:41

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, a

2条回答
  •  难免孤独
    2021-01-22 18:39

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

提交回复
热议问题