Exception in thread "main" java.util.Inputmismatchexception

Asked

Viewed 2,513 times

2

I made a simple program, just to calculate a mathematical function to 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\8.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 get a double instead of float (because I need to do a good calculation ), but the program gives the same problem, someone has some idea how to whether to solve this?

Note: If I put integers as 1 or 0, the program runs normally, but when placing a more precise number like 0.2 or 0.0038 for example, appears this exception.

2 answers

1


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

  • I am using netbeans and am using with stitch

  • @Rez try with comma, here it worked.

  • @Rez has to be with the same comma!

  • @Rez I spoke, but I corrected right after I took the test. Using the comma, should fix your problem.

  • Ow vlws kara,é even,put with comma in netbeans and the stop gave no problem

  • @Rez need, just come here and give a shout. Hugs. Good Luck!

Show 1 more comment

0

According to the documentation:

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

This happens because the decimal separator of your system is represented by a comma and not a point. Exchange it by comma partially solves your problem if you run your code on a system where the decimal separator is a dot, the problem occurs again.

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


You may prefer to place the code in a block Try/Catch and inform the user that the entered value 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 treat the raised exception using the NumberFormat to format the value in a pattern where the dot is recognised as 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.