0

Why does Java throw an error when using Scanner.nextFloat() but not Scanner.nextInt() ?

package myshit;

import java.lang.Math;
import java.util.Scanner;

public class speed2 {
    public static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args){
        float number = keyboard.nextFloat();
        System.out.print("Start");

        }
    }

Input:

2.5

Output:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextFloat(Unknown Source)
at myshit.speed2.main(speed2.java:10)

But just by switching nextFloat to nextInt no error occurs:

package myshit;

import java.lang.Math;
import java.util.Scanner;

public class speed2 {
    public static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args){
        int number = keyboard.nextInt();
        System.out.print("Start");

        }
    }

input:

3

Output:

Start

What am I doing wrong?

Appears i needed to input , instead of . Seems to be because of Eclipse

PandaDeTapas
  • 516
  • 1
  • 7
  • 16
  • your code doesn't give me an error. try again and recompile. see if that works. – lacraig2 Mar 11 '15 at 20:54
  • The answer is correct that you need to use a comma, but it doesn't tell why: because your locale expects floating point numbers in that style. For example French or German locales do that. This is not related to your IDE, but to the default locale configured in your installed Java. See the duplicate for more information. – Tom Jun 15 '21 at 11:19
  • Nice package name! – MorganS42 Jul 02 '23 at 20:49

2 Answers2

4

You should type it like 2,5 not 2.5 ( i think this only happens in Netbeans, funny fact that it get parsed to 2.5 )

run:
2,5
Your number is 2.5

Using the notation 2.5 in Netbeans.

run:
2.5

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextFloat(Scanner.java:2345)
at test.Test.main(Test.java:25)
Enpi
  • 281
  • 1
  • 9
  • This was obviously my problem because now it works, dunno why though? Thankyou so much! – PandaDeTapas Mar 11 '15 at 21:03
  • Actually it should be 2.5 , but NetBeans has a strange behaviour in that case. – Enpi Mar 11 '15 at 21:04
  • I tried it in Eclipse, it happens the same. Interesting Java error you did just discover by the way. – Enpi Mar 11 '15 at 21:18
  • This issue isn't related to NetBeans or Eclipse, this comes from your installed Java. Your default locale expects numbers in that way. Such locales are for example French or German. See the duplicate link above the question so solutions on how to fix it programmatically. – Tom Jun 15 '21 at 11:22
0

I hope you are not trying to give input 2.5f or something for a float from the command line... because then you get an error..

apart from that your code works fine as you can see if you add to print the number you give as input:

import java.lang.Math;
import java.util.Scanner;

public class speed2 {
    public static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args){
        System.out.print("Give me float:");
        float number = keyboard.nextFloat();
                System.out.print("You gave me the number: " + number);


        }
    }

OUTPUT

Give me float:2.5
You gave me the number: 2.5
adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • 1
    maybe it has something to do with the locale you are using... messing up commas and dots for decimals.. try : String.format(Locale.US, "%f", floatValue) – adrCoder Mar 11 '15 at 21:09
  • I'm not sure how to implement this on the input? I'm a beginner at Java so... You can't format input can you? – PandaDeTapas Mar 11 '15 at 21:13
  • 1
    You are right but you can change Eclipse locale : http://stackoverflow.com/questions/4947484/how-to-set-eclipse-console-locale-language – adrCoder Mar 11 '15 at 21:19