Skip to content

Do…while is not working in input verification

So I’m trying to do an input verification for my program in java, and I’m trying to do that in this setter:

  public void setClientName(String clientName) {
        boolean valid;
            valid = false;
                    this.clientName = clientName;
                     throw new IllegalArgumentException("Invalid client name");
            }catch(Exception e){
                System.out.println("Invalid name");
                valid = true;

But when I call it and put a wrong name, the do…while does not work and the program just continue

Here’s where I call it

  public void openAccount(int i){
        nCartao = 2021120040 + i;
        System.out.println("Account Number : " + (nCartao));
        System.out.println("Client Name :");

        setClientName(; // I CALL IT HERE

        System.out.println("Client Age : ");
        age = sc.nextInt();
        System.out.println("Balance :");
        balance = sc.nextInt();

What am I doing wrong?



Maybe that’s because in your catch you are stating that valid is true when it should be false to repeat the block.

User contributions licensed under: CC BY-SA
3 People found this is helpful