2016-02-04 85 views
-1

我一直在试图弄清楚为什么这段代码不能工作几个小时,而且我对ArrayList方法相当陌生。基本上,方法不起作用,我不确定为什么。编译器说“无法找到方法 - 方法isLong”。isLong()方法不起作用

错误:

import java.util.Scanner; 
import java.io.*; 
import java.util.ArrayList; 
public class BookCollection 
{ 
    private ArrayList<Book> bookList; 
    public BookCollection() throws Exception 
    { 
     bookList = new ArrayList<Book>(); 
     String firstLine, isbnI, authorI, areaI; 
     int lengthI; 
     Book book; 
     Scanner FILE, fileScan; 
     fileScan = new Scanner(new File("Books.txt")); 
      while (fileScan.hasNext()) 
      { 
       firstLine = fileScan.nextLine(); 
       FILE = new Scanner(firstLine); 
       isbnI = FILE.next(); 
       authorI = FILE.next(); 
       areaI = FILE.next(); 
       lengthI = FILE.nextInt(); 
       book = new Book(isbnI, authorI, areaI, lengthI); 
       bookList.add(book); 
      } 
    } 
    public void displayLongBooks() 
    { 
     System.out.println("LONG BOOKS\n\n"); 
     for(int i = 0; i < bookList.size(); i++) 
     { 
     if (bookList.get(i).isLong()) 
     { 
      return System.out.println(bookList.get(i)+"\n"); 
     } 
     } 
    } 
    void displayBooksFromAuthor(String Author) 
    { 
     for (int i = 0; i < bookList.size(); i++) 
     { 
     if (bookList.get(i).author.equals(Author)) 
      System.out.println(bookList.get(i)); 
     } 
    } 
    void displayBooksFromArea(String Area) 
    { 
     for (int i = 0; i < bookList.size(); i++) 
     { 
     if (bookList.get(i).area.equals(Area)) 
      System.out.println(bookList.get(i)); 
     } 
    } 
    Book longestBook() 
    { 
     int maxId = 0; 
     int currMax = 0; 
     for(int i=0; i < bookList.size(); i++) 
     { 
      Book currBook = bookList.get(i); 
      if(currBook.getLength() > currMax) 
      { 
      currMax = currBook.getLength(); 
      maxIdx = i; 
      } 
     } 
     return bookList.get(maxIdx); 
    } 

} 
` 

isLong方法:

import java.util.Scanner; 
import java.io.*; 
import java.util.ArrayList; 

class Book 
{ 
    String isbn, author, area; 
    int length; 

    public Book(String isbn, String author, String area, int length) 
    { 
     this.isbn = isbn; 
     this.author = author; 
     this.area = area; 
     this.length = length; 
    } 

    boolean islong() 
    { 
     if(length >= 400) 
     return true; 
     else 
     return false; 
    } 

String toString() 
    { 
    String s; 
    S = "[BOOK ISBN: " + this.isbn + ", AUTHOR: " + this.author + ", AREA: " + this.area + ", PAGES: " + this.length + "]\n"; 
    return s; 
    } 
public String getIsbn() 
{ 
    return this.isbn; 
} 

public String getAuthor() 
{ 
    return this.author; 
} 

public String getArea() 
{ 
    return this.area; 
} 

public int getLength() 
{ 
    return this.length; 
} 


} 
` 

主要方法:

` 
import java.util.Scanner; 
public class TestBookCollection 
{ 
    public static void main(String[] args) throws Exception 
    { 
    Scanner scan = new Scanner(System.in); 
    BookCollection testBooks = new BookCollection(); 

    System.out.println(testBooks); 
    System.out.println(); 
    testBooks.displayLongBooks(); 
    System.out.println("\nChoose an author: "); 
    String author = scan.next(); 
    testBooks.displayBooksFromAuthor(author); 
    System.out.println("\nChoose an area: "); 
    String area = scan.next(); 
    testBooks.displayBooksFromArea(area); 
    } 
} 
` 
+1

你的方法叫做islong ...那里没有L。 – user3284549

+1

Java标识符区分大小写:你调用'isLong',但你声明'islong'。 – Kenney

+1

另请注意:使用布尔表达式来选择true/false是完全多余的 - 直接使用表达式:“return length> = 400;” – Durandal

回答

1

你声明的方法islong而不是作为。