Sami
Sami

Reputation: 105

Accept only 8-digit numbers

I have this assignment and I need help when I try to change sales value it does not change and when I enter the Id for salesperson must be accept 8 digit numbers not more or less I tryed .....but I stoped here.

else {
               System.out.print("Please enter your ID : ");
               String ID = scanner.nextLine();
               //Declaration
               boolean exists = checkIfIDExists(ID);
               if(exists) {
                   System.out.print("Please enter new sales value : $");
                   float newSalesValue = scanner.nextFloat();

                   Iterator<Salesperson> iterator = salespersons.iterator();
                   while(iterator.hasNext()) {
                       Salesperson salesperson = iterator.next();
                       if(salesperson.getID().equals(ID)) {
                           salesperson.setAnnualSales(newSalesValue);
                       }
                   }
               }

accapet 8 digit numbers

}
           else {
               System.out.print("Please enter your ID : ");
               String ID = scanner.nextLine();

               boolean ifValid = validateID(ID, salespersons);

               if(MAX_ID_CHAR_COUNT != ID.toCharArray().length) {
               } else {
                   System.out.print("Please enter eight digits of your ID : ");
                   ID = scanner.nextLine();

                   // Declaration and loop
               }
              //if = validateID(ID, salespersons);
               if(ifValid) {                      
                   System.out.print("Duplicate digits. Please enter eight digits of your ID : ");
                      ID = scanner.nextLine();

               }

Upvotes: 1

Views: 489

Answers (1)

lonecloud
lonecloud

Reputation: 413

using a loop to validate you id is valid ,that is a code

   public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        boolean ifValid=false;
        String ID;
        //using loop to validate this id is valid and length is 8
        do{
            System.out.print("Please enter your ID : ");
            ID = scanner.nextLine();
            ifValid= validateID(ID, salespersons);
            if (!ifValid){
                System.out.print("Duplicate digits. Please enter eight digits of your ID : ");
            }
        }while (!ifValid);
        System.out.print("Please enter eight digits of your ID : ");
        ID = scanner.nextLine();
    }

    private static boolean validateID(String id, Object salespersons) {
        return id!=null&&8==id.length();
    }

Upvotes: 2

Related Questions