Problems retrieving the length of a vector (.lenght) [closed]

-1

I am currently encountering the following situation in my main program:

public static void main(String[] args) {
        Scanner wen= new Scanner(System.in);

        int numeroCuenta;
        int i;
        System.out.println("BIENVENIDO! ");
        System.out.println("Favor ingrese  el número de cuenta ");
        numeroCuenta=wen.nextInt();

        int vector[] = new int[5];


        if(numeroCuenta == vector.length) {
            System.out.println ("NIP aceptado:"+numeroCuenta);
        } else {
            System.out.println ("El numero  debe tener  5 digitos");
        }                  
    }

Why can not I retrieve "vector.length"? Thanks in advance. Greetings.

    
asked by asuna 28.12.2017 в 05:51
source

1 answer

1

The problem I see is that you are comparing an Integer type number such that "12345" with a length of 5 and that's why it never works for you. If you enter a 5, you will only enter the if and accept the PIN. But I guess it's not what you want, so try the following code:

public static void main(String[] args) {
        Scanner wen= new Scanner(System.in);

        int numeroCuenta;
        int i;
        System.out.println("BIENVENIDO! ");
        System.out.println("Favor ingrese  el número de cuenta ");
        numeroCuenta=wen.nextInt();

        int vector[] = new int[5];

        String num = numeroCuenta +"";

        if(num.length() == vector.length) {
            System.out.println ("NIP aceptado:"+numeroCuenta);
        } else {
            System.out.println ("El numero  debe tener  5 digitos");
        }  

    }
    
answered by 28.12.2017 в 12:08