-3

I have this problem where I want to identify the clients with in id. I want that Id only to be a number. Any ideas? Thanks in advance.

    int id = IBIO.inputInt("Enter the clients id:");

    boolean Clientexists = clientsAdm.existsClient(id) > 0;
    if (Clientexists != true) {

        String name = IBIO.inputString("Enter the client name:");
        String surname = IBIO.inputString("Enter the client surname:");
        String address = IBIO.inputString("Enter the client address:");
        String phone = IBIO.inputString("Enter the client phone:");
        String CreditCard = IBIO.inputString("Enter the type of card the client has: green, red or gold");  //asegurar que solo se ingrese RED, GREEN or GOLD
        Client c = new Client(name, surname, address, phone, CreditCard, id);

        clientsAdm.addClient(c);
    } else {

        IBIO.output("Error, this ID already exists.");

    }

    IBIO.output("Your client has been added.");
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

This could work, if you gave me more details maybe I could help you better a greeting

       int id = IBIO.inputInt("Enter the clients id:");

        boolean Clientexists = clientsAdm.existsClient(id) > 0;
        if (Clientexists != true) {
                String name = IBIO.inputString("Enter the client name:");
            String surname = IBIO.inputString("Enter the client surname:");
            String address = IBIO.inputString("Enter the client address:");
            String phone = IBIO.inputString("Enter the client phone:");
            String CreditCard = IBIO.inputString("Enter the type of card the client has: green, red or gold");  //asegurar que solo se ingrese RED, GREEN or GOLD
            Client c = new Client(name, surname, address, phone, CreditCard, id);


if(id instanceof Integer) {
clientsAdm.addClient(c);
} else {
IBIO.output("Id is not valid");
}
        } else {
                IBIO.output("Error, this ID already exists.");
            }
            IBIO.output("Your client has been added.");
    }

Else you can try this:

 int id;
    try{
    id = IBIO.inputInt("Enter the clients id:");}
    catch(Exception e){
IBIO.output("Id is not valid");
    }
ChRix
  • 11
  • 1
  • 6
0

If zero is an illegal input for an ID, then you can check if the ID is zero, because IBIO.inputInt(String prompt) returns zero when prompt is given a non-integer.

If zero is a legal input for an ID, then IBIO.inputInt(String prompt) cannot be used. When a non-integer is passed into IBIO.inputInt, the exception is masked, and a zero is returned. Instead, you will need to define your own method for reading a user input. You can do something like this:

static int inputIntWithRetry(String prompt) {
   int result = 0;
   int numberOfRetry = 5; // holds the number of times to reprompt the user
   boolean isInt = false; // flag for exiting the loop early if user provides a int
   for(int i = 0; i < numberOfRetry || !isInt; i++) {
      try {
         result = Integer.valueOf(
         input(prompt).trim()).intValue();
         isInt = true;
      } catch (Exception e) {
         System.out.println("Id is not valid");
      }
   }
   if (isInt) {
      return result;
   } else {
      // handle not receiving an int after 5 tries
   }
}
Gordan Lin
  • 27
  • 1
  • 6