Weird behaviour with Scanner#nextFloat

北城以北 提交于 2019-11-27 09:05:15

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!