-1
由於某種原因在這個程序中的任何地方,我的ArrayList返回的大小爲0,這真的使我在完成程序時感到困惑。我無法完成程序,直到我得到修復。爲什麼我的ArrayList在程序中無處不返回大小0?
下面是程序:
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
public class Book
{
//Instance Variables
private String author;
private static String title;
/*ArrayList<Books> books = new ArrayList<Books>();*/
public static void main(String[] args) throws Exception
{
ArrayList<String> books = new ArrayList<String>();
fillArray(books);
getBook(books);
/* getTitle(books);
clearScreen();
getFile(books);
printMenu();
chooseOption(books);
display(books);
/* exit();*/
}
private static void printArrayList(ArrayList<String> books)
{
for (int i = 0; i < books.size(); i++){
System.out.println(books.get(i));
}
}
private static void fillArray (ArrayList<String> books) throws Exception
{
File file = new File("library.dat");
BufferedReader br = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line != null) {
lines.add(line.replace(">", ""));
line = br.readLine();
}
for(String l : lines) {
System.out.println(l);
}
}
public static void clearScreen()
{
System.out.println("\u001b[H\u001b[2J");
}
private static void exit()
{
System.exit(0);
}
private static void printMenu()
{
System.out.println("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("\t\tTHE GREAT BOOKS SEARCH PROGRAM");
System.out.println("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("\t1) Display all book records");
System.out.println("\t2) Search for a book by Title");
System.out.println("\t3) Exit Search Program");
System.out.println("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
private static void chooseOption(ArrayList<String> books)
{
System.out.print("\tPlease Enter Your Choice > ");
Scanner stdin = new Scanner(System.in);
String option = stdin.nextLine();
if (option.compareTo("1") == 0){
printRecord(books);
display(books);}
else if (option.compareTo("2") == 0){
showSearch(books, "Moby Dick");}
else if (option.compareTo("3") == 0){
System.out.println("\n\tGoodbye. Have a nice day. :-)");
System.exit(0);}
}
public static void printRecord(ArrayList<String> books)
{
clearScreen();
for (int i = 0; i < books.size(); i++){
System.out.println("\tRecord #" + books.get(i));}
System.out.println("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("\tTitle : ");
System.out.println("\tAuthor's Name : ");
System.out.println("\tCopyright : ");
System.out.println("\tPrice : ");
System.out.println("\tGenre : ");
System.out.println("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println();
}
private static void display(ArrayList<String> books)
{
Scanner in = new Scanner(System.in);
for(int i = 0; i< books.size();i ++)
{
printRecord(books);
System.out.println("");
System.out.print("\tPress enter to continue, or type M to go back to the menu > ");
if(in.nextLine().equals("M"))
break;
clearScreen();
}
}
public static void showSearch(ArrayList<String> books, String key)
{
System.out.print("\n\tSearch Title > ");
Scanner stdin = new Scanner(System.in);
String Stitle = stdin.nextLine();
System.out.println(binarySearch(books, Stitle));
}
public static void getFile(ArrayList<String> books)
{
System.out.println("\t\tTHE BOOK SEARCH PROGRAM");
System.out.println("__________________________________________________________________ _____________");
System.out.println();
System.out.println("\tWhat file is your book data stored in?");
System.out.println();
System.out.println("\tHere are the files in the current directory : \n");
//Get all files from directory
File curDir = new File(".");
String[] fileNames = curDir.list();
ArrayList<String> data = new ArrayList<String>();
//Find files which may have data.
for(String s:fileNames)
if(s.endsWith(".dat"))
data.add(s);
for (int i = 0; i < data.size(); i++){
System.out.println(data.get(i));
}
System.out.print("\n\tFilename: ");
Scanner stdin = new Scanner(System.in);
String dataName = stdin.nextLine();
int arraysize = books.size();
if (dataName.equals("library.dat")){
Collections.sort(books);
System.out.println("\n\tA total of " + arraysize + " have been input & sorted by title.");}
else {
System.out.println("Error");}
System.out.print("\n\tPlease Hit Return to Continue...");
Scanner stdin2 = new Scanner(System.in);
String st = stdin2.nextLine();
clearScreen();
}
public static void getBook(ArrayList<String> books) throws Exception
{
int totalElements = books.size();
// while (dataScanner.hasNext())
File datafile = new File("library.dat");
Scanner datascanner = new Scanner (datafile);
String data = datascanner.nextLine();
StringTokenizer dataParser = new StringTokenizer(data, ";");
title = dataParser.nextToken();
String author = dataParser.nextToken();
String CopyR = dataParser.nextToken();
String Price = dataParser.nextToken();
String Genre = dataParser.nextToken();
System.out.println(totalElements);
for (int i=0; i< books.size(); i++){
System.out.println(title);
System.out.println(author);
System.out.println(CopyR);
System.out.println(Price);
System.out.println(Genre);
}
}
public static String getTitle(ArrayList<String> books) throws Exception
{
String n = "n";
for (String str : books)
{
StringTokenizer st = new StringTokenizer(str, ";");
String title = st.nextToken();
}
return title;
}
private static int binarySearch (ArrayList<String> books, String key)
{
Collections.sort(books);
int index = Collections.binarySearch(books, key);
return index;
}
/*
public Book (String title, String author, String CopyR, String Price)
{
this.title = title;
this.author = author;
this.CopyR = CopyR;
this.Price = Price;
}
*/
}