Exception in thread "main" java.util.InputMismatchException

2

I made a simple program, just to calculate a mathematical function so I get an exact result, but there were some complications in Java.

Digite o valor de x:
0.2
Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Scanner.java:909)
  at java.util.Scanner.next(Scanner.java:1530)
  at java.util.Scanner.nextFloat(Scanner.java:2388)
  at funçaofx.funx.main(funx.java:15)
C:\Users\Renan\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53:
Java returned: 1 FALHA NA CONSTRUÇÃO (tempo total: 2 segundos)

Code:

package funçaofx;

import java.util.Scanner;

public class funx {

    public static void main(String args[]){
        float x;
        float y;
        Scanner input = new Scanner(System.in);

        System.out.println("Digite o valor de x:");
        x = input.nextFloat();
        input.nextLine();
        y = (x - ((x*(x - 1))/(2*x)));
        System.out.println("valor calculado por F(x):"+y);
        input.close();
    }

}

I tried to put double instead of float (because I have to do a good calculation However, the program gives the same problem, does anyone have any idea how if this is resolved?

Note: If I put integers as 1 or 0 , the program executes normally, but by entering a more precise number as 0.2 or 0.0038 For example, this exception appears.

    
asked by anonymous 29.08.2016 / 03:37

2 answers

1

If its value is 2.5 enter as input 2,5 using comma , .

    
29.08.2016 / 03:45
0

According to the documentation:

  

The exception InputMismatchException is thrown by the instance of    Scanner when the token retrieved does not match the type   expected. InputMismatchException extends from class    NoSuchElementException , which is used to indicate that the element   requested does not exist.

This is because your system's decimal separator is represented by a comma, not a dot. Swapping it with comma solves your problem partially, if you run your code on a system where the decimal separator is a dot , the problem reoccurs.

Note : You can change this setting in the Control Panel → Clock, Language, and Region → Change date, time, or number formats → Additional Settings. >

You may want to put the code in a block Try/Catch and tell the user that the value you entered is incorrect.

float x, y;

try(Scanner input = new Scanner(System.in)) {
    System.out.println("Digite o valor de x: ");

    try {
         x = input.nextFloat();

         y = (x - ((x* (x - 1)) / (2* x)));
         System.out.println("valor calculado por F(x): " + y);

    } catch (InputMismatchException err) {
         System.out.println("Erro! O valor digitado não é válido. Tente novamente!");
         // err.printStackTrace();
    }
}

See DEMO

Another alternative is to handle the thrown exception using NumberFormat to format the value in a pattern where period is recognized as a decimal separator.

float x, y;

NumberFormat nformat = NumberFormat.getInstance(Locale.GERMAN);
Scanner input = new Scanner(System.in);

System.out.println("Digite o valor de x: ");

try {
     x = input.nextFloat();
} catch (InputMismatchException err) {
     x = nformat.parse(input.nextLine()).floatValue();
     // err.printStackTrace();
}

y = (x - ((x* (x - 1)) / (2* x)));

System.out.println("valor calculado por F(x): " + y);
input.close();

See DEMO

    
29.08.2016 / 16:40