Question

I want the user to input a customerID and a videoID (the first field in the array) but I want the program to alert the user that they entered an incorrect ID I have tried Arrays.asList(...).contains(...) but does not work

// The video array
videos[0]=new Video("150", "Bagdad by Night", 6.00,         5);
videos[1]=new Video("151", "Lord of the Rings 1", 5.00,         0);

//The customer array
customers [0]= new Customer("9902JI", "Innes    ", 0,43484001);
customers [1]= new Customer("8906RH", "Herbert", 0,43484000);

public static void HireVideo(){

    System.out.println("Enter Customer ID");
    String customerID = sc.next();
    System.out.println("Enter Video ID");
    String videoID = sc.next();

    HireList.add(new Hire(customerID, videoID));
}

I am trying to access it using a method in another class the method is:

public int getCustomer(String IdToFind) {               
    for (int index = 0; index<Driver.customers.length && Driver.customers[index]!=null;index++) {
        if (Driver.customers[index].getCustomerID().equals(IdToFind))
            return index; //ID found                                        
    }                                                   
    return -1; //ID not found
}
Was it helpful?

Solution

As Mentioned by ifLoop, equalsIgnoreCase() should work. Also the check for Driver.customers[index]!=null should not be as part of For check because the loop will terminate if any of the records returns null, giving an impression that the id is not found. But in reality, there can be a record after that null record, matching the condition. Better still, we should make sure the Customer Array never have a record which is null.

public int getCustomer(String IdToFind){   
  if(Driver.customers.length > 0) {            
    for (int index = 0; index<Driver.customers.length;index++){
      if (Driver.customers[index].getCustomerID().equalsIgnoreCase(IdToFind))
         return index;         //ID found                                      
     }
    return -1;   //ID not found
   } else {
      return -1  //ID not found, as no customers in driver object
   }                              
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top