0

in my code i have created an array:

static BankClient clients[] = new BankClient[MAXCLIENTS]; // This array holds the clients up to a max of 1000

what i need to do is check this array to see if it contains a specific int. i was able to do this successfully through the use of a for loop and an if statement as shown below:

System.out.println("\n Ask for ID");
    System.out.println(">");
    IDInput = input.nextInt();              
    System.out.println("Check ID \n");

    for (int i = 0; i < clients.length; i++) 
    { 
        BankClient managementOptions = clients[i];
        
        if (managementOptions.getID() == IDInput) 
        {   
            // code in here                                 
        } 

however, once i tried to put an else statement at the bottom to write an error message to the user, as a result of an invalid id, it proceed to print out an error for all ids saved in the array that do not match the id given by the user. this is what i wanted:

else if (// check if IDInput doesnt exist within the client array)
        {
            System.out.println("Error. Client does not exist.");
        }

however i cant seem to figure out what to put between the brackets. any help would be super appreciated! still pretty new to this website so if you need anymore information i will try my best to add it.

3 Answers 3

1

Simply keep track in the loop if you've found something:

boolean found=false;
for (int i = 0; i < clients.length; i++) 
    { 
        BankClient managementOptions = clients[i];
        
        if (managementOptions.getID() == IDInput) 
        {   
            found=true;                       
            break; // no need to search further
        } 
}
// no element found
if (!found){
 System.out.println("Error. Client does not exist.");
}

You cannot just print the error in the loop since you need to be sure you've looked at the whole array.

Sign up to request clarification or add additional context in comments.

5 Comments

this worked in terms of not printing out an error for every object in the array, but it still crashes when i enter something that doesn't exist in the array e.g the array has values 1,2,3,4 but i enter 7. how could i fix this? thanks for the help!
Define "crash". What happens? What exception and stack trace do you get?
this is the whole error that appears: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "groupCoursework.BankClient.getID()" because "managementOptions" is null at groupCoursework.UserInterface.managementOptions(UserInterface.java:84) at groupCoursework.UserInterface.main(UserInterface.java:27) this appears when i enter something that doesnt exist in the array
Ah yes because some elements in the array are null. just have if (managementOptions!=null && managementOptios.getID() == IDInput) to only check the ID if the element is not null
And also if you can don't use Arrays. Use an ArrayList that can grow as needed and that would only contains non null values.
0
System.out.println("\n Ask for ID");
System.out.println(">");
IDInput = input.nextInt();              
System.out.println("Check ID \n");

boolean isPresent = false;
for (int i = 0; i < clients.length; i++) { 
  BankClient managementOptions = clients[i];
     
  if (managementOptions.getID() == IDInput) {   
      
      isPresent = true 
      
      // code in here                                 
  }
}

if (! isPresent) {
  System.out.println("Error. Client does not exist.");
} 

Comments

0

Simple solution via Stream API:

Boolean isPresent = Arrays.stream(clients).anyMatch(x -> x.getID().equals(IDInput));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.