Weird behaviour with Scanner#nextFloat

后端 未结 1 1119
慢半拍i
慢半拍i 2020-12-01 16:51

Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input:

price = sc.nex         


        
相关标签:
1条回答
  • 2020-12-01 17:24

    I wonder if you're not handling an end of line token appropriately. Often if you use Scanner#next###() (except for nextLine), and you reach an end of line token as when the user presses enter, if you don't handle the end of line token, it will prevent the Scanner object from working appropriately. To solve this, call Scanner#nextLine() when this token needs to be handled. If you post some of your code, we can see if this indeed is your problem and if my suggestion offers a solution.

    edit: nope, you're not using System.in so this is not the problem. On the other hand, you do need to set the locale of the Scanner before accepting the French number. i.e.,

         sc = new Scanner(frenchDecimal);
         sc.useLocale(Locale.FRENCH);
         price = sc.nextFloat();
    
    0 讨论(0)
提交回复
热议问题