Okay, so this is my final assignment for a class I am taking. I'm making a Book Library Database that keeps track of all books that are entered and changed, things like that. I figured a useful feature would be saving your data in a text file so that it could be loaded again any time. I successfully managed to get the program to save my data into a text file but I can't seem to Load that text file in a way that the lines are assigned to their correct variables again. I'll attach some code... (WARNING: It is 500 lines of code, so you can compile it just to get a feel of how it's intended to work, the classes that are giving me trouble are at the end of the code)
import hsa.Console;
import java.io.*;
/*This program was created
*by *anonymous* to act as
*a book rental system. The
*user can input, add, delete,
*modify, rent, return,
*and search books.
*/
public class librarysystem
{
static Console c;
static String name[] = new String [1001]; //These were made global because they are present in a large majority of the methods.
static String type[] = new String [1001];
static String Genre1[] = new String [1001];
static String Genre2[] = new String [1001];
static String Availability[] = new String [1001];
static int x, books;
public static void main (String[] args)throws IOException //Main Menu
{
c = new Console (); //Opens the console.
int choice;
do
{
c.print ("\n Main Menu\n");
c.print (" *********\n");
c.print (" 1. Enter New Books");
c.print ("\n 2. Add More Books");
c.print ("\n 3. Modify Book Data");
c.print ("\n 4. Delete Book Data");
c.print ("\n 5. Rent ");
c.print ("\n 6. Return");
c.print ("\n 7. Search for Books");
c.print ("\n 8. Save Data");
c.print ("\n 9. Load Data");
c.print ("\n 10. Exit");
c.print ("\n Select your choice 1-10: ");
choice = c.readInt (); //Displays selection and implements a method based on the users selection.
if (choice == 1)
EnterBooks (); //opens data entry method
if (choice == 2)
AddBooks (); //opens add more Books method
if (choice == 3)
ModifyBooks (); //Opens modify data method
if (choice == 4)
DeleteBook (); //Opens delete data method
if (choice == 5)
Rent (); //Opens rent method
if (choice == 6)
Return (); //Opens return method
if (choice == 7)
SearchBooks (); //opens search method
if (choice == 8)
SaveBooks(); //Runs Save method
if (choice == 9)
LoadBooks (); //Runs Load method
if (choice == 10)
{
c.print ("\nGoodbye!"); //exits program
}
else if (choice < 1 || choice > 10) //If user makes an invalid choice, this displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
while (choice != 10); //The menu will continue to loop until a valid number is entered or the user inputs 7, which exits the program.
}
public static void EnterBooks () //User enters initial Books in this method
{
int userselection;
c.clear ();
c.print ("WARNING, DATA PREVIOUSLY ENTERED MAY BE OVERWRITTEN. DO YOU WISH TO CONTINUE?\n\n"); //The user has an option to do this, as data that was previously entered will be overwritten.
c.print ("1. Continue");
c.print ("\n2. Main Menu\n");
c.print ("Please make your selection (1-2): ");
userselection = c.readInt ();
if (userselection == 1)
{
c.clear ();
c.print ("How many books do you wish to enter?: "); //User may choose how many new Books they wish to enter.
books = c.readInt ();
for (x = 1 ; x <= books ; x++)
{
c.print ("\nPlease enter the name of book " + x + ":"); //Data Entry for all required values of x
name [x] = c.readLine ();
c.print ("Please enter this books type: ");
type [x] = c.readLine ();
c.print ("Please enter this books primary genre: ");
Genre1 [x] = c.readLine ();
c.print ("Please enter this books secondary genre: ");
Genre2 [x] = c.readLine ();
Availability [x] = ("Available"); // All new Books are assumed to be available, to change their status to rented, use the rent method.
}
if (userselection == 2)
c.clear ();
else if (userselection < 1 || userselection > 2) //If the user enters an invalid selection, the following displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
}
public static void AddBooks () //User can add more Books after the initial Books have been entered.
{
int userselection;
c.clear ();
c.print ("ONLY USE THIS FEATURE AFTER ENTERING NEW VIDEOS, ELSE THIS DATA WILL BE LOST!\n\n"); //The user has an option if they want to use this, because using
c.print ("1. Continue"); //this feature before entering initial data can cause data to be overwritten.
c.print ("\n2. Main Menu\n");
c.print ("Please make your selection (1-2): ");
userselection = c.readInt ();
if (userselection == 1) //If user wishes to continue, the program will run the full method, otherwise it will return to the main menu.
{
for (x = 1 ; x <= books ; x++)
c.clear ();
books = books + 1; //Adds another array place holder.
c.print ("Please enter the name of the new book: "); //All Data entered is printed.
name [books] = c.readLine ();
c.print ("Please enter this books type: ");
type [books] = c.readLine ();
c.print ("Please enter this books primary genre: ");
Genre1 [books] = c.readLine ();
c.print ("Please enter this books secondary genre: ");
Genre2 [books] = c.readLine ();
Availability [books] = ("Available");
}
if (userselection == 2)
c.clear ();
else if (userselection < 1 || userselection > 2) //If the user enters an invalid selection, the following displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
public static void ModifyBooks () //User can modify data already entered into the program.
{
c.clear ();
String modBook;
int flag, userselection;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("Database\n");
for (x = 1 ; x <= books ; x++)
{
c.print ("Name: " + name [x] + " Type: " + type [x] + " Genre: " + Genre1 [x] + " " + Genre2 [x] + " Availability: " + Availability [x] + "\n"); //If a record is flagged, it is displayed.
}
c.print ("\nWhat is the name of the book you wish to modify?: ");
modBook = c.readLine ();
c.print ("ARE YOU SURE YOU WANT TO MODIFY THIS?\n\n"); //The user has an option to do this, as data that was previously entered will be overwritten.
c.print ("1. Yes");
c.print ("\n2. No\n");
c.print ("Please make your selection (1-2): ");
userselection = c.readInt ();
if (userselection == 1)
{
c.clear ();
for (x = 1 ; x <= books ; x++)
{
if (modBook.compareToIgnoreCase (name [x]) == 0) //Compares values in the array to user input
{
flag = x; //An array value in name[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false) //If no results are found, this displays
{
c.println ("Book Not Found");
}
else //If a result is found, this displays.
{
c.clear ();
c.print ("Please enter the name of the modified book: "); //All Data entered is printed.
name [flag] = c.readLine ();
c.print ("Please enter this books modified type: ");
type [flag] = c.readLine ();
c.print ("Please enter this books modified primary genre: ");
Genre1 [flag] = c.readLine ();
c.print ("Please enter this books modified secondary genre: ");
Genre2 [flag] = c.readLine ();
Availability [flag] = ("Available");
}
}
if (userselection == 2)
c.clear ();
else if (userselection < 1 || userselection > 2) //If the user enters an invalid selection, the following displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
public static void DeleteBook () //User can delete data already entered into the system.
{
c.clear ();
String delbook;
int flag, userselection;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("Database\n");
for (x = 1 ; x <= books ; x++)
{
c.print ("Name: " + name [x] + " Type: " + type [x] + " Genre: " + Genre1 [x] + " " + Genre2 [x] + " Availability: " + Availability [x] + "\n\n"); //If a record is flagged, it is displayed.
}
c.print ("What is the name of the book you wish to delete?: ");
delbook = c.readLine ();
c.print ("ARE YOU SURE YOU WANT TO DELETE THIS?\n\n"); //The user has an option to do this, as data that was previously entered will be overwritten.
c.print ("1. Yes");
c.print ("\n2. No\n");
c.print ("Please make your selection (1-2): ");
userselection = c.readInt ();
if (userselection == 1)
{
c.clear ();
for (x = 1 ; x <= books ; x++)
{
if (delbook.compareToIgnoreCase (name [x]) == 0) //Compares values in the array to user input
{
flag = x; //An array value in name[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false) //If no results are found, this displays
{
c.println ("Book Not Found");
}
else //If a result is found, this displays.
{
for (x = flag ; x <= books ; x++)
{
name [x] = name [x + 1];
type [x] = type [x + 1];
Genre1 [x] = Genre1 [x + 1];
Genre2 [x] = Genre2 [x + 1];
}
books = books - 1; // Deletes flagged book from database.
}
}
if (userselection == 2)
c.clear ();
if (userselection < 1 || userselection > 2) //If the user enters an invalid selection, the following displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
public static void Rent () // Users can change the availability status of any available Book in the database to "Rented"
{
c.clear ();
String rentbook, selection;
int flag;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("Available Books\n\n");
for (x = 1 ; x <= books ; x++)
{
if (Availability [x] == "Available")
{
c.print (name [x] + "\n");
}
}
c.print ("\nWhat is the name of the book you wish to rent?: ");
rentbook = c.readLine ();
for (x = 1 ; x <= books ; x++)
{
if (rentbook.compareToIgnoreCase (name [x]) == 0) //Compares values in the array to user input
{
flag = x; //An array value in name[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false) //If no results are found, this displays
{
c.println ("Book Not Found");
}
else //If a result is found, this displays.
{
if (Availability [flag] == "Rented")
{
c.print ("This book is already under rent. Sorry!\n"); //Book is already rented, user cannot rent it again until it has been returned.
}
if (Availability [flag] == "Available")
{
c.print ("You have rented the book: " + name [flag] + "\n"); //Book is available, you can rent it. Changes status to "rented"
Availability [flag] = ("Rented");
}
}
}
public static void Return () //Users can return any "rented" Book in the database to "available status.
{
c.clear ();
String returnbook, selection;
int flag;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("Rented Books\n\n");
for (x = 1 ; x <= books ; x++)
{
if (Availability [x] == "Rented")
{
c.print (name [x] + "\n");
}
}
c.print ("\nWhat is the name of the book you wish to return?: ");
returnbook = c.readLine ();
for (x = 1 ; x <= books ; x++)
{
if (returnbook.compareToIgnoreCase (name [x]) == 0) //Compares values in the array to user input
{
flag = x; //An array value in name[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false) //If no results are found, this displays
{
c.println ("Book Not Found");
}
else //If a result is found, this displays.
{
if (Availability [flag] == "Available") //If the book is already available, then the book hasnt been rented, meaning you can't return it.
{
c.print ("This book is already available in our database!\n");
}
if (Availability [flag] == "Rented") //This book is rented, therefore you can return it.
{
c.print ("You have returned the book: " + name [flag] + "\n");
Availability [flag] = ("Available");
}
}
}
public static void SearchBooks () //A menu where the user can select one of three search options: Name, Type, and Genre.
{
int choice;
c.clear ();
c.print (" Search Menu\n");
c.print (" *********\n");
c.print (" 1. Search by Name");
c.print ("\n 2. Search by Type");
c.print ("\n 3. Search by Genre");
c.print ("\n 4. Main Menu\n");
choice = c.readInt ();
if (choice == 1)
NameSearch (); //opens data entry method
if (choice == 2)
Ratesearch (); //opens search method
if (choice == 3)
Genresearch (); // Opens genre search method
if (choice == 4)
c.clear ();
else if (choice < 1 || choice > 4) //If the user enters an invalid selection, the following displays.
{
c.clear ();
c.print ("Invalid Number, try again!\n\n");
}
}
public static void NameSearch () //User can check for Books by name to gather information on them and check availability.
{
c.clear ();
String findname;
int flag;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("What is the name of the book you wish to find?: ");
findname = c.readLine ();
for (x = 1 ; x <= books ; x++)
{
if (findname.compareToIgnoreCase (name [x]) == 0)
{
flag = x; //An array value in name[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false)
{
c.println ("\nBook Not Found\n"); //If the search returns no results, this displays
}
else
{
c.print ("\nBook Found: " + name [flag] + " Type: " + type [flag] + " Genre: " + Genre1 [flag] + " " + Genre2 [flag] + " Availability: " + Availability [flag] + "\n\n"); //If a record is flagged, it is displayed.
}
}
public static void Ratesearch () //User can check for Books by type to gather information on them and check availability.
{
c.clear ();
String findtype;
int flag;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("What is the type of the book you wish to find?: ");
findtype = c.readLine ();
for (x = 1 ; x <= books ; x++)
{
if (findtype.compareToIgnoreCase (type [x]) == 0)
{
flag = x; //An array value in type[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false)
{
c.println ("\nBook Not Found\n"); //If the search returns no results, this displays
}
else
{
c.print ("\nBook Found: " + name [flag] + " Type: " + type [flag] + " Genre: " + Genre1 [flag] + " " + Genre2 [flag] + " Availability: " + Availability [flag] + "\n"); //If a record is flagged, it is displayed.
}
}
public static void Genresearch () //User can check for books by genre to gather information on them and check availability.
{
c.clear ();
String findgenre;
int flag;
boolean found;
flag = 0; //flag is 0 for string comparison purposes.
found = false; //Found is false until proven true.
c.print ("What is 1 Genre of the book you wish to find?: "); //The user may enter 1 genre they are interested in.
findgenre = c.readLine ();
for (x = 1 ; x <= books ; x++)
{ //This will run through both genres of all books in order to find a match.
if (findgenre.compareToIgnoreCase (Genre1 [x]) == 0)
{
flag = x; //An array value in Genre1[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
if (findgenre.compareToIgnoreCase (Genre2 [x]) == 0)
{
flag = x; //An array value in Genre2[x] has been flagged for use.
found = true; //The search produced a result, and is now true
}
}
if (found == false)
{
c.println ("\nBook Not Found\n"); //If the search returns no results, this displays
}
else
{
c.print ("\nBook Found: " + name [flag] + " Type: " + type [flag] + " Genre: " + Genre1 [flag] + " " + Genre2 [flag] + " Availability: " + Availability [flag] + "\n\n"); //If a record is flagged, it is displayed.
}
}
public static void SaveBooks () throws IOException
{
c.clear ();
PrintWriter BookData;
BookData = new PrintWriter (new FileWriter ("Books.txt"));//Create new file, will contain passing marks.
for (int x = 1 ; x <= books ; x++)
{
BookData.println(name[x]);
BookData.println(type[x]);
BookData.println(Genre1[x]);
BookData.println(Genre2[x]);
BookData.println(Availability[x]);
}
BookData.close (); //Close Books.txt
}
public static void LoadBooks () throws IOException
{
String line;
BufferedReader LoadData;
LoadData = new BufferedReader (new FileReader ("Books.txt")); // Retrieves file for reading
c.clear ();
for (int x = 0 ; x <= books ; x++) //Loops until every line of the file has been read.
{
for (x = 0 ; x <= 9 ; x++)
{
line = LoadData.readLine (); //Assign file a string value.
c.print (line + "\n"); //Prints the file for user to read.
}
}
LoadData.close (); //Close file.
}
}
LoadBooks() is the problem I'm having. I don't want it to display all of the books, I just want the lines of the text file to be assigned back to their proper variables. For example, the first line in the text file will be the name of the first book entered, how can I assign it back to the array name{1] and the next line to type{1], etc.?