Weird behaviour with Scanner#nextFloat

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

Leave a Comment