Specific entry of information

Asked

Viewed 39 times

0

The goal is to make the program accept only numbers in the method numero() (cannot accept if it is letter, point, question mark, etc).

Already in the method usuario(), letter only (cannot accept numbers, point, question mark, 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");
    }

}
  • 1

    Take a look at this question and its answer: https://answall.com/q/42351/132

  • I did not understand, the statement seemed to me very confusing. Would it be as examples of what should be accepted/refused by these methods? And as you call them in the main program?

1 answer

0


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

in this way your Voce can put a constructor to ensure that it receives only the expected type and can remove characters as in the example I did also 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");
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.