开发者

Searching in a java arraylist without returning a value

Ive been trying to change my method to a void. But whenever I change it, it always prints out the book name and an error message. How can I change my method to a void?

public int displayBook开发者_运维技巧Details(String bookName) {
  for (int i = 0; i < classrooms.size(); i++) {
    Library library = librarys.get(i);
    if (library.returnBookName().equals(bookName)) {
      System.out.println("Index: " + i);   
      System.out.println(library.returnBookName());
      System.out.println(library.authorName());           
      return i;
    }
  }
  return -1;
  System.out.println ("Book name is not valid"); 
}


You need to remove the return statements (and replace the first one by a simple return (no parameter)) too.

public void displayBookDetails(String bookName) {
  for (int i = 0; i < classrooms.size(); i++) {
    Library library = librarys.get(i);
    if (library.returnBookName().equals(bookName)) {
        System.out.println("Index: " + i);   
        System.out.println(library.returnBookName());
        System.out.println(library.authorName());           
        //removed  return i;
        return;
    }
  }

  // removed return -1;
  System.out.println ("Book name is not valid"); 
}


Try something like this, I am sure it will work:

public void displayBookDetails(String bookName) {
  for (int i = 0; i < classrooms.size(); i++) {
    Library library = librarys.get(i);
    if (library.returnBookName().equals(bookName)) {
        System.out.println("Index: " + i);   
        System.out.println(library.returnBookName());
        System.out.println(library.authorName());           
        return; // modified here
    }
  }
  // modified here
  System.out.println ("Book name is not valid"); 
}


You probably still want to return when you've found the book - that may be what you've missed before:

public void displayBookDetails(String bookName) {
  for (int i = 0; i < classrooms.size(); i++) {
    Library library = librarys.get(i);
    if (library.returnBookName().equals(bookName)) {
      System.out.println("Index: " + i);   
      System.out.println(library.returnBookName());
      System.out.println(library.authorName());           
      return;
    }
  }
  System.out.println ("Book name is not valid"); 
}

Personally I'd probably separate out the "search" from "display":

public Library getBookDetails(String bookName) {
  for (int i = 0; i < classrooms.size(); i++) {
    Library library = librarys.get(i);
    if (library.returnBookName().equals(bookName)) {
       return library;
    }
  }
  return null;
}

public void displayBookDetails(String bookName) {
  Library bookDetails = getBookDetails(bookName);
  if (bookDetails == null) {
    System.out.println ("Book name is not valid"); 
  } else {
    System.out.println(bookDetails.returnBookName());
    System.out.println(bookDetails.authorName());           
  }
}

(Note that there's no such thing as the "index" at this point, of course. If you really need to display that as well, it would need to be part of Library.)


You can do so by using return without a value:

    System.out.println(library.authorName());           
    return;
}


The return type you are trying to change to void only signifies what is returned by the method. However the method might have different side-effects like I/O, e.g. printing to the console. Unfortunately there is no way to constrain side-effects in Java or most OOP langauges. Functional programming aims at getting rid of all side effects in your functions, so it can be reasoned about the program more easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜