Error entering a double with period as decimal separator

0

If I enter it with a comma (eg 3.5), it works well.

package metodosnumericos;
import java.util.Scanner;
public class derivada {

    public static void main(String[] args) 
    {
        //int coeficiente, potencia,x,xc,xp;
           Scanner entrada = new Scanner (System.in);
           double a;
           System.out.println("Ingresa");
         //  a=Double.parseDouble(entrada.nextLine());
           a=entrada.nextDouble();
           System.out.println(a);
}
}
    
asked by Shaz 17.10.2017 в 03:27
source

2 answers

1

Estimated nextDouble() uses the decimal points and separators of thousands of the language.

In Spanish the point is separator of miles and comma the decimales .

Try using the useLocale () function and pass an English Locale, as follows:

entrada.useLocale(Locale.ENGLISH);
    
answered by 17.10.2017 в 03:45
0

You can do it this way, replacing Scanner with BufferedReader and getting line by line.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Derivada {

public static void main(String[] args) throws IOException  {
    BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));           
       double a = 0d;
       System.out.println("Ingresa un valor: ");
       a = Double.parseDouble(teclado.readLine());
       //Se interpreta que teclado.readLine(), obtiene el texto escrito hasta que se pulsa enter
       System.out.println(a);
  }
} 

Check it out, I think it should work.

    
answered by 17.10.2017 в 07:42