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.?

Edited 4 Years Ago by gomonkeyninja

Load that text file in a way that the lines are assigned to their correct variables again

Can you write a smaller, simpler program that only tries to read the file and put its lines into the variables where you want them to be? It'll be easier to work with. When it works you can merge it back into this large program.

Can you describe the format of the data in the file? What is on each line?
Then describe where the data from each line is supposed to go in the program's variables.

Edited 4 Years Ago by NormR1

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 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, allows file to be displayed.
    c.clear ();
    for (int x = 0 ; x <= books ; x++) //It needs to read 5 lines for every book, that is one problem I'm having
    {
    name[x] = LoadData.readLine();
    type[x] = LoadData.readLine();
    Genre1[x] = LoadData.readLine();
    Genre2[x] = LoadData.readLine();
    Availability[x] = LoadData.ReadLine();
    }
    }
    LoadData.close (); //Close file.
}


}

I've broken it down into the main features of the program. Entering books, saving them to a file, and loading them again. The first 2 methods work as intended, but not the third (LoadBooks). You are saying that I should write a new program that my main program can call upon to assign the lines in the text file to their proper variables? I'd have to think a bit on how to do that. But anyways when I use the save function with an example, you would see something like this in the text file:

Hunger Games (name)
Novel (type)
Fantasy (Genre1)
Adventure (Genre2)
Available (Availability)
Catching Fire (name of book 2)
Novel (type of book 2)
Fantasy (Genre1 of book 2)
Adventure (Genre 2 of book 2)
Available (Availability of book 2)
etc.....

The saving feature works fine, I just want theLoadBooks() method to read the first line of the text file and set it back to its original array variable "name{1]", the second line will be "type{1]".
Sorry if I'm not explaining it very well, I'm pretty stressed for time right now.

Edited 4 Years Ago by gomonkeyninja

It looks like there are 5 lines in the file for each book.
I assume there are 5 parallel arrays that will hold the data for each book. The code needs to read the 5 lines and save each into its array and then move the array index to the next slot for the next 5 lines and do it again.

Edited 4 Years Ago by NormR1

Ok I've solved the LoadData problem, but now something even more confusing has happened. My rent and return methods work fine before loading data from a file, but after loading the data they stop functioning. My search methods work fine though. All of those methods search the database and flag a result so they are structured almost identically. Why would some methods stop working while others act as usual?

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 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.
        }
    }

Here I've included the Rent Method and the Search method. BOTH work before I use the Load Method. Only the search method works AFTER I use the load method. I have no clue as to why this has happened.

I think I fixed the LoadData method, but after I use the LoadData method, my rent and return methods stop working, and ONLY those two methods.

public static void Rent ()  // Users can change the availability status of any available Book in the database to "Rented"


{
    c.clear ();
    String rentbook;
    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 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.
    }
}

I've included the rent method and a search mehod. The search method works perfectly whether I load data or not. The rent method doesn't work if I load the data using LoadData method. If I choose not to save/load data and enter from scratch the rent/return methods work fine. I have no idea why this is.

Edited 4 Years Ago by gomonkeyninja

Please explain what "stop working" means?
Try debugging the code by adding println statements to the methods that print out the values of variables as they are used and changed so you can see what the program sees as it executes.

You need to use the equals() method to compare String objects NOT the == operator.

Why are your array and loop indexes starting at 1 vs 0?

Before using the LoadData method, I could run the Rent method, and it would display all data files with an Availability status of "Available", and then I could select any title from that list of data files to change its Availability status to "Rented, (vise versa for the Return Method). AFTER using the LoadData method, no data files display as "available" and it's like there are no data files in the program at all, which isn't true because the other methods work like normal.
The == operators are how I was taught to compare the string values in my course, and they work perfectly in my search methods, so I'm confused as to why these other 2 methods are any different?
The loop indexes were just laziness on my part, they still perform the same function but I've changed them back to starting at 0.

The == operators are how I was taught to compare the string values i

That is totally wrong. The equals() method is for comparing the contents of objects like Strings.

Have you tried debugging the code by adding println statements to show what the code is seeing and doing?

Hmm, I don't know. The compiler I'm using doesn't seem to have a problem with it until I run that method...
In the rent method I've added a println statement that will print the flag ( which is 0 ) if a data file is found. I loaded the database and entered the name of a book in my database and it printed a 0. Which I'm assuming it means that the method DOES realise the database is there, and it is able to compare the strings, but it just won't print them. Sorry if I'm a little slow at this, it's all a learning process for me.

Print out the values of the variables that are being compared so you see what the program sees. For example : S....o...println("Av=" + Availability [flag] + "<");

The compiler I'm using doesn't seem to have a problem with it

Its when the code is executed that there is a problem. == is used to detect if two variables refer to the same object.
It does not look at the contents of an object.

Edited 4 Years Ago by NormR1

Sorry for bumping, but I'd feel bad if I didn't thank you for your help. I found out the issue, and it did have to do with the if statements that allowed for comparison. I needed to add .compareTo statements to do the comparison, it was simply poor structure.
So thank you! :)
*problem solved

This question has already been answered. Start a new discussion instead.