Java isLong()方法不工作
我一直在试图弄明白为什么这段代码已经好几个小时没用了,而且我对ArrayList方法还是相当陌生的。基本上,Java isLong()方法不工作,java,Java,我一直在试图弄明白为什么这段代码已经好几个小时没用了,而且我对ArrayList方法还是相当陌生的。基本上,isLong方法不起作用,我不确定原因。编译器说“找不到方法-方法isLong” 错误: import java.util.Scanner; import java.io.*; import java.util.ArrayList; public class BookCollection { private ArrayList<Book> bookList; p
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);
}
}
`
主要方法:
`
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);
}
}
`
您将方法声明为
islong
而不是islong
您的方法称为islong。。。这里没有L。Java标识符区分大小写:您调用isLong
,但声明了isLong
。还要注意:使用布尔表达式选择true/false是完全多余的-直接使用表达式:“返回长度>=400;”哇。。非常感谢你,我不敢相信我竟然没有意识到。。。
`
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);
}
}
`