My exception created by my own java poo does not work

0
public static void main(String[] args) throws IOException {
    Operaciones.cargarDatosClientes();
  Operaciones.imprimirDatos();
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 *
 * @author alumno-203
 */
public class Operaciones {

    private static Cliente listaCliente[] = new Cliente[2];
    private static BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
    private static Scanner entrada1 = new Scanner(System.in);

    public static void cargarDatosClientes() throws IOException {
        System.out.println("**INGRESE DATOS DEL CLIENTE**");
        for (int i = 0; i < listaCliente.length; i++) {
            System.out.println("----------------------------------------");
            Cliente cliente = new Cliente();
            try {
                System.out.println("Nombre Completo");
                cliente.setNombreCompleto(entrada.readLine());
            } catch (Excepcion e) {
                e.mensajePersonalizado();
            }

            System.out.println("Nro Cuenta");
            cliente.setNumeroCuenta(entrada.readLine());
            try {
                System.out.println("Saldo inicial");
                cliente.setSaldoInicial(entrada1.nextInt());
                System.out.println("Total articulo");
                cliente.setTotalArticulos(entrada1.nextInt());
                System.out.println("Total creditos");
                cliente.setTotalCreditos(entrada1.nextInt());
                System.out.println("Limite credito");
                cliente.setLimiteCredito(entrada1.nextInt());
            } catch (InputMismatchException e) {
                System.err.println("Error se debe ingresar un valor numerico ");
            }

            listaCliente[i] = cliente;
        }

    }

    public static void imprimirDatos() {
        for (int i = 0; i < listaCliente.length; i++) {
            long nroSaldo = (listaCliente[i].getSaldoInicial() + listaCliente[i].getTotalArticulos()) - listaCliente[i].getTotalCreditos();
            System.out.println("---------------------------------------------");
            if (nroSaldo > listaCliente[i].getLimiteCredito()) {
                System.err.println("El cliente " + listaCliente[i].getNombreCompleto() + " se excedio el límite de credito ");
                System.out.println("");
            } else {
                System.out.printf("El cliente " + listaCliente[i].getNombreCompleto() + " no se excedio al límite de credito");
                System.out.println("");
            }

        }
    }
}
----------------------------------------------------
public class Excepcion extends NumberFormatException {

    public Excepcion() {

        super();
    }

    public Excepcion(String string) {
        super(string);
    }

    public void mensajePersonalizado() {

        System.err.println("Error: Dato ingreso incorrecto, ingrese solo caracteres.");
    }
}    

I do not know why it does not work. I have to do it this way for a college assignment.

    
asked by Agustin Ozuna Leguizamón 13.05.2018 в 02:11
source

2 answers

0

You must add a control in the method setNombreCompleto that validates you if the string received as a parameter contains numbers or not. I would do it like this (using regular expressions):

public void setNombreCompleto(String valor) throws Excepcion {
    //Pattern y Matcher pertenecen a java.util.regex
    Pattern p = Pattern.compile("[0-9]+"); //Expresión regular para buscar cualquier secuencia de números
    Matcher m = p.matcher(valor);
    if (m.matches()) {
        throw new Excepcion("El nombre introducido no es válido, contiene números");
    } 
}

Obviously, that pattern and that matcher could be declared as final static global variables of the Client class, since they do not cease to be constant.

Greetings

    
answered by 13.05.2018 / 08:28
source
0

I imagine that client has the field nombre of type String .

and class Excepcion inherit from NumberFormatException

The class NumberFormatException captures the exceptions when the field you are trying to enter is of type Number (long, float, int, double, etc) and can not be formatted or casted when you read the String.

how to prove the exception:

also asks for the account number inside the try. and rectify:

try {
      System.out.println("Nombre Completo");
      cliente.setNombreCompleto(entrada.readLine());

      System.out.println("Ingrese el Nro Cuenta");
      cliente.setNumeroCuenta(entrada.readLine());
} catch (Excepcion e) {
      e.mensajePersonalizado();
}

EYE! the format type of the numeroCuenta attribute must be in a numeric data type for the function to work, in this way if you enter a text like:

> 'Ingrese el Nro de cuanta': 
> hola mundo

You'll generate the exception you're looking for

    
answered by 13.05.2018 в 02:30