Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:41:04+00:00 2026-06-12T12:41:04+00:00

For some reason everywhere in this program, my ArrayList is returning a size of

  • 0

For some reason everywhere in this program, my ArrayList is returning a size of 0 which really messes me up in finishing the program. I can not finish the program until I get this fixed.

Here is the program:

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;
}
*/
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:41:05+00:00Added an answer on June 12, 2026 at 12:41 pm

    The answer is the obvious one: you never call books.add() or books.addAll(). Not once.

    I think you meant to implement fillArray something like this instead:

    private static void fillArray (ArrayList<String> books) throws Exception
    {
        File file = new File("library.dat");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = br.readLine();
        while(line != null) {
            books.add(line.replace(">", ""));
            line = br.readLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
For some reason I can't detect I can't set a z index for any
For some reason this isn't working, am I missing something obvious? RewriteRule ^(.*)infopopup.html$ /acatalog/infopopup.html
For some reason beyond me my html page is not finding my stylesheet. I'm
I have looked everywhere and surprisingly can't find a good solution to this! I've
I've looked EVERYWHERE for this, can't find anything. I basically need to store an
For some reason the toggleClass function does not seem to be working. I have
I've checked everywhere in my code and I could not find the reason why
I have a program which is not written by me. I dont have its
I'm really new to Backbone, and I've looked everywhere to try and figure this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.