Error control Scanner.hasNextDouble

0

I'm doing this program but I always jump to error even if you enter the correct parameter, do you know why it can be?

/*
* To change this license header, choose License Headers in Project       Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package estructuresderepeticio;

/**
*
* @author carlos
*/
import java.util.Scanner;

public class JudoNew3 {

    public static void main(String[] args) {

        Scanner Scan = new Scanner(System.in);

        Double pes;


        boolean pesReal = false;



        System.out.println("digam el pes: ");

        pesReal = Scan.hasNextDouble();

        if (pesReal) {
            pesReal = false;

            pes = Scan.nextDouble();

            if (pes > 25 && pes < 65) {
                System.out.println("Peso registrado. ");
                pesReal = true;

            } else {
                System.out.println("El niño esta fuera del peso permitido ");

            }
        } else {
            System.out.println(" Peso incorrecto, introduzca un numero real");
        }

    }

}
    
asked by Carlos 17.10.2016 в 08:53
source

1 answer

1

I do not know what error gives you ... And a detail, the name of the variables is lowercase, that is, scan > OKAY; Scan > No, since it is a variable. Your code would be:

package help;

import java.util.Scanner;

public class JudoNew3 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Double pes;
        boolean pesReal = false;

        System.out.println("digam el pes: ");
        pesReal = scan.hasNextDouble();
        if (pesReal) {
            pesReal = false;

            pes = scan.nextDouble();
            if (pes > 25 && pes < 65) {
                System.out.println("Peso registrado. ");
                pesReal = true;

            } else {
                System.out.println("El niño esta fuera del peso permitido ");

            }
        } else {
            System.out.println("Peso incorrecto, introduzca un numero real");
        }

        // Cierra el flujo de Scanner.
        scan.close();
    }
}

The output with non-numeric data:

digam el pes: 
ge
Peso incorrecto, introduzca un numero real

The output with incorrect numbers (Use "." and not ","):

digam el pes: 
54.2
Peso incorrecto, introduzca un numero real

The output with a correct data, but out of range:

digam el pes: 
19999,4
El niño esta fuera del peso permitido 

And the output with correct data and within range:

digam el pes: 
50,0
Peso registrado. 

I certainly do not see that there is an error.

  

Information:   When we generate code, Java interprets the decimal values with a ".", That is, if we want to give value to:

double miValorDecimal = 1.5;// Usamos "." y no ","
  

Now, in the execution it's different ... why? because we have the   keyboard configured to Spanish, therefore, in console, decimals   they happen to be as we always use them, that is to say, in console we will put:

Introduzca un valor decimal:
6,5

This is how decimals work in Java and in the Java console, it is a colloquial way of saying it, to know how to configure eclipse to avoid this, or to use specialized methods, that is another matter entirely. We have San Google.

    
answered by 17.10.2016 / 09:05
source