Specific input

0

The goal is to get the program to only accept numbers in the numero() method (you can not accept it if it is a letter, period, question, etc.).

In the usuario() method, only letters (can not accept numbers, period, question, etc.).

 public void numero() {
        System.out.println("Digite o número da conta: ");
        this.setNumeroConta(sc.nextInt());
        sc.nextLine();
        if (this.getNumeroConta() >= 0) {
            System.out.println("SUCESSO_NA_OPERÇÃO NUMERO_DA_CONTA");
        }
    } else {
        System.out.println("Somente números");
    }
}

 public void usuario() {
        System.out.println("Digite o nome de usuário: ");
        this.setDono(sc.nextLine());
        if (this.getDono() != null) {
            System.out.println("SUCESSO_NA_OPERÇÃO NOME_DO_USUARIO");
        }
    } else{
        System.out.println("Somente letras");
    }

}
    
asked by anonymous 03.11.2017 / 22:43

1 answer

0

It will depend a lot on where the information comes from, if it comes from a screen (java web) you can use the html5 attribute called pattern with the regex that only takes letters pattern="[a-zA-Z] + / g "and so for just numbers pattern=" [0-9] + / g ", or if it comes from the database you can use a coalesce formatting the way you want it.

In this method you can put a constructor to ensure that it receives only the expected type and can remove characters as in the example I did with regex.

 public void usuario(String entrada.matches("^[a-zA-Z\s]+$")) {
            System.out.println("Digite o nome de usuário: ");
        this.setDono(sc.nextLine());
        if (this.getDono() != null) {
            System.out.println("SUCESSO_NA_OPERÇÃO NOME_DO_USUARIO");
        }
    } else{
        System.out.println("Somente letras");
    }

}
    
04.11.2017 / 03:42