I have this system which I only want to try and borrow books from a library.
I have data already in arrays.
I need to check that nobody else has a book then add it to the data.
I have commented where i think the problem is.
i have tried syste.out and I jst get the original data that was in the array instead of the added data???
any ideas??

thanks for looking

the code is below

import javax.swing.*;
 class LibraryMain {
   //  Creates a staff of employees
   public static void main (String[] args)    {
   	 //  Sets up the list of books.
   	 Books[] BookList;
   	 BookList = new Books[5];
   	 BookList[0] = new Books("title", "author", "B1");
   	 BookList[1] = new Books("my", "by","B2");
   	 BookList[2] = new Books("lname", "by","B3");
              BookList[3] = new Books("lname", "by","B4");
              BookList[4] = new Books("lname", "by","B5");
   	
  System.out.println("Library System\n");
  //creates object to print out details
	Borrowers personnel = new Borrowers();
    personnel.records();
     System.exit(0); //exits
   }
    
}

class Borrowers{
  
  public Students[] BorrowersList;
   public Borrowers ()   {
//  Sets up the list of borrowers.
BorrowersList = new Students[4];
BorrowersList[0] = new Students ("adam", "S1","", "");
BorrowersList[1] = new Students ("tim", "S2","B1", "");
BorrowersList[2] = new Students ("Mr", "L3","B5", "");
BorrowersList[3] = new Students ("mrs", "L4","B3", "B5");
         
 for(int count = 0;count<1;count++){ //loop to continue to enter books
 //gets student ID
 String num = JOptionPane.showInputDialog(null,
		     	   "Enter the ID number",
			       "Library System",
			        JOptionPane.QUESTION_MESSAGE);
			      
        // gets bookid
         String num2 = JOptionPane.showInputDialog(null,
		    "Enter the Book ID",
			"Library System",
			 JOptionPane.QUESTION_MESSAGE);
			  //finds if any one has that book
			    for (int count2	=0; count <  4; count++)      {
         	if (BorrowersList[count2].bookid1.equals("num2")){
          			System.out.println("Book already on loan");
          				   break;
          						}
          		             else{
          				num = BorrowersList[count2].bookid1;
          				// problem here, wont override array??
          			    }
			       }
			    }
			 
	 }
			
 public void records()   {
 	//prints out borrowers details
    for (int count=0; count < BorrowersList.length; count++)      {
         System.out.println (BorrowersList[count]);
         System.out.println ("-----------------------------------");
       
      }
   }
}

 class Students{
   protected String name;
   protected String ID;
   protected String bookid1;
   protected String bookid2;

   //  Sets up students with the information.
   public Students (String sName, String sID, String sBookid1, String sBookid2)   {
      name = sName;
      ID = sID;
      bookid1 = sBookid1;
      bookid2 = sBookid2;
   
   }
   //  Returns information about the students as a string.
   public String toString()   {
   	String result = "Name           : " + name + "\n";
           result += "ID            : " + ID + "\n";
           result += "Books borrowed: " +  bookid1 + bookid2 ;
     

      return result;
   }
   
}

class Books{
   protected String title;
   protected String author;
   protected String ISBN;
	  //  Sets up books with the information.
  public Books (String sName, String sID, String sBookid)   {
      title = sName;
      author = sID;
      ISBN = sBookid;
   
   }
   
}

Code tags added. -Narue

Dani AI

Generated

Nice little starter project. The reason your updates do not “stick” is that you are changing local variables instead of mutating the student object that lives inside the array, and a couple of your comparisons/loop indices are off. already pointed out the wrong comparator and the index mix‑up; the other design fix is to separate the steps: find the student, check if the book is already on loan, then write the change back to that student record.

A small helper class makes the intent obvious and avoids repeating loops. Drop something like this in and call it from your event/dialog code.

class LibraryOps {
  static Students findStudent(Students[] list, String id) {
    for (int i = 0; i < list.length; i++) {
      Students s = list[i];
      if (s != null && id.equals(s.ID)) return s;
    }
    return null;
  }

  static Students whoHasBook(Students[] list, String isbn) {
    for (int i = 0; i < list.length; i++) {
      Students s = list[i];
      if (s == null) continue;
      if (isbn.equals(s.bookid1) || isbn.equals(s.bookid2)) return s;
    }
    return null;
  }

  static boolean addBookIfSpace(Students s, String isbn) {
    if (s.bookid1 == null || s.bookid1.length() == 0) { s.bookid1 = isbn; return true; }
    if (s.bookid2 == null || s.bookid2.length() == 0) { s.bookid2 = isbn; return true; }
    return false; // already holding two
  }
}

Usage example in your borrow flow:

  • Look up the borrower: Students stu = LibraryOps.findStudent(BorrowersList, enteredId);
  • Reject if not found.
  • Check availability: Students holder = LibraryOps.whoHasBook(BorrowersList, enteredIsbn);
  • If holder != null, the book is already on loan.
  • Otherwise: boolean ok = LibraryOps.addBookIfSpace(stu, enteredIsbn); and handle the “no space left” case.

Two more quick tips:

  • Store empty slots as null instead of "". It simplifies checks.
  • Avoid comparing to string literals like "num2"; compare to the variable that holds the user’s input. This is exactly what was getting at, and it is the core reason you were seeing only the original array data.

First of all, I strongly&&friendly recommend formatting your code using a beautifier, like Jacobe:
(it's free for Windows and Linux).
It will make the program's flow much more obvious.

I found and fixed several issues (first 3 are listed below):

1)

    for (int count2 =0; count <  4; count++)      {
        if (BorrowersList[count2].bookid1.equals("num2"))

It should be count2 < 4; count2++)

2)

num = BorrowersList[count2].bookid1;

It should be:

BorrowersList[count2].bookid1 = num2;

because you update the list, not the num2's value.

3)

if (BorrowersList[count2].bookid1.equals("num2"))

It should be

if (BorrowersList[count2].bookid1.equals(num2))

because you compare with num2's value, not with the string "num2".
For instance, if num2 = "Book2", then "num2" is still "num2", not "Book2".
By the way, I believe it's easier to put Book1 / Student 1 instead of B1 / adam.
At least for me, it makes the code easier to read.

I tested the program in two cases:

Case 1:
    Input:
        S1
        Book5

    Output: 
        Sorry, the book is not available...
        Student1 has no books.

Case 2:
    Input:
        S1
        Book2

    Output:
        Student1 has Book2

Although the program works, it does not verify if a student has already one book, so, if Student2 borrows Book2, Book2 will replace Book1, instead of adding it as the second book. Of course, this could be fixed, if you want that. For now, I just want to make sure the program works.

Should you need more details, I'll be glad to provide them.

I attached the code in LibraryMain.java

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.