I am trying to add a searchBook method that finds all the books donated as donatedBy data attribute. I have to call the searchBook method to find the books donated by "Daisy". I am having trouble finding out where to put the searchBook method so that I can call it later when I get the user input for "donor".
I have provided a snippet of my code for your convenience. Thank you very much!
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter number of Books: "); int N = scanner.nextInt(); scanner.nextLine(); Books[] books = new Books[N]; for (int i = 0; i < N; i++) { System.out.print("Enter the title: "); String title = scanner.nextLine(); System.out.print("Enter the donor: "); String donor = scanner.nextLine(); System.out.print("Enter the number of chapters: "); int chaps = scanner.nextInt(); System.out.print("Enter the book location: "); bookLocation = scanner.nextLine(); scanner.nextLine(); books[i] =new Books(title, donor, chaps, bookLocation); } boolean flag=false; System.out.print("\nFor search enter donor's name: "); String donor = scanner.nextLine(); for(int i=0; i<books.length;i++){ Books b=books[i]; if (b.getDonatedBy().equalsIgnoreCase(donor)){ //if the user input matches the donor name, do the following System.out.print("\nBook "+(i+1)+": \n"); b.printDetails(); //you're printing if the user input matches the donor flag=true; } } if (flag){ System.out.println("\nTask Complete. "); } scanner.close(); } }
https://stackoverflow.com/questions/66925400/how-to-invoke-a-searchbook-method-to-find-books-donated-as-donatedby-data-attrib April 03, 2021 at 05:23AM
没有评论:
发表评论