Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have error in output we the user should add her surname and lastname(in student and faculty)
package librarysystem;

/**
 *
 * @author maryam
 */
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;

public class LibraryDerive {
static ArrayList<Student> studentList=new ArrayList<Student>();
static ArrayList<Faculty> FacultyList=new ArrayList<Faculty>();
static ArrayList<Book> bookList=new ArrayList<Book>();
static ArrayList<Book> borroeList=new ArrayList<Book>();
   public static void main(String[] args) throws NumberFormatException, IOException {
       Scanner br = new Scanner(System.in);
       int reapeat=13;
       while(reapeat>0) {
           System.out.println("------------------------------------------------------");
           System.out.println("-------WELCOME TO OUR LIBRARY MANAGEMENT SYSTEM-------");
           System.out.println("------------------------------------------------------");
           System.out.println("----------Please choose one of below Options----------");
           System.out.println("1. Add Student");
           System.out.println("2. Add Faculty");
           System.out.println("3. Add Book");
           System.out.println("4. Display All Students");
           System.out.println("5. Display All Faculty");
           System.out.println("6. Display All Books");
           System.out.println("7. Search Student");
           System.out.println("8. Search Faculty");
           System.out.println("9. Search Book");
           System.out.println("10. Borow Book");
           System.out.println("11. Display All Borow Book");
           System.out.println("12. Return Book");
           System.out.println("13. Exit");
           System.out.print("your choice: ");

           int option = br.nextInt();
           switch (option) {
           case 1:
           System.out.println("Enter Student Id");
           int id = br.nextInt();
           boolean chekId=checkStudentId(id);
           if(chekId) {
               System.out.println("StudentId Already Exist");
               break;
           }
           System.out.println("Enter Student surName");
           String surname= br.nextLine();
           System.out.println("Enter Student lastName");
           String lastName= br.nextLine();
           System.out.println("Enter Student Age");
           int age = br.nextInt();
           System.out.println("Enter Student Sex :");
           String sex= br.nextLine();
           Student student=new Student(id, surname, lastName, age, sex);
           studentList.add(student);
           break;
           case 2:
           System.out.println("Enter Faculty Id");
           int id1 = br.nextInt();
           boolean chekId1=checkFacultyId(id1);
           if(chekId1) {
               System.out.println("FacultyId Already Exist");
               break;
           }
           System.out.println("Enter Faculty surName");
           String surname1= br.nextLine();
           System.out.println("Enter Faculty lastName");
           String lastName1= br.nextLine();
           System.out.println("Enter Faculty Age");
           int age1 = br.nextInt();
           System.out.println("Enter Faculty Sex :");
           String sex1= br.nextLine();
           Faculty Faculty=new Faculty(id1, surname1, lastName1, age1, sex1);
           FacultyList.add(Faculty);
           break;


           case 3:
               System.out.println("Enter Book Id :");
               int bookId = br.nextInt();
               boolean chekBookId=checkBookId(bookId);
               if(chekBookId) {
                   System.out.println("Book Id Already Exist");
                   break;
               }
               System.out.println("Enter Book tittle");
                String tittle= br.nextLine();
               System.out.println("Enter Book Purchase Date");
                String purchaseDate= br.nextLine();
               System.out.println("Enter Book Availability :");
               String status= br.nextLine();
               Book b = new Book(bookId, tittle, purchaseDate, status);
               bookList.add(b);
           break;
           case 4:
           displayAllStudent();
           break;
           case 5:
           displayAllFaculty();
           break;
           case 6:
           displayAllBook();
           break;
           case 7:
           System.out.println("Please Enter student Id, you want to Search");
           int searchStudentId = br.nextInt();
           searchStudent(searchStudentId);
           break;
           case 8:
           System.out.println("Please Enter Faculty Id, you want to Search");
           int searchFacultyId = br.nextInt();
           searchFaculty(searchFacultyId);
           break;
           case 9:
               System.out.println("Please Enter Book Id, you want to Search");
               int searchBookId = br.nextInt();
               searchBook(searchBookId);
           break;
           case 10:
           borrowBook();
           break;
           case 11:
               DisplayborrowBook();
               break;
           case 12:
               returnBook();
               break;
           case 13:
               reapeat=-13;
               break;
           }
          
       }

   }
     private static void returnBook() throws NumberFormatException, IOException {
       Scanner br = new Scanner(System.in);
       System.out.println("---------------------");
       System.out.println("Please enter Student Id:");
       int id = br.nextInt();
       System.out.println("Please enter Book Id:");
       int bookId = br.nextInt();
       for(int i=0; i<borroeList.size(); i++) {
               if(borroeList.get(i).getBookId()==bookId){
                      
                   borroeList.get(i).setStatus("AVAILABLE");
                   borroeList.remove(i);
                   break;
               }
           }
         
      
   }
   private static void DisplayborrowBook() {
       for(Book book :borroeList) {
          
               System.out.println("Book Id:"+book.getBookId()+" Tittle :"+book.getTittle()+" Auhtor Surname:"+book.getAuthorSurname()+
                       " Author FirstName:"+book.getAuthorFirsName()+" Purchase Date:"+book.getPurchaseDate()+" staus:"+book.getStatus());
              
          
       }
   }
   private static void borrowBook() throws NumberFormatException, IOException {
       Scanner br = new Scanner(System.in);
       System.out.println("----List of all Available book as Below----");
       for(Book book :bookList) {
           if(book.getStatus().equalsIgnoreCase("AVAILABLE")) {
               System.out.println("Book Id:"+book.getBookId()+" Tittle :"+book.getTittle()+" Auhtor Surname:"+book.getAuthorSurname()+
                       " Author FirstName:"+book.getAuthorFirsName()+" Purchase Date:"+book.getPurchaseDate()+" staus:"+book.getStatus());
              
           }
       }
       System.out.println("---------------------");
       System.out.println("Please enter Student/Faculty Id:");
       int id = br.nextInt();
       System.out.println("Please enter Book Id:");
       int bookId = br.nextInt();
       for(Book book :bookList) {
               if(book.getBookId()==bookId){
                       borroeList.add(book);
                       book.setStatus("Not AVAILABLE");
               }
           }
   }
   private static void searchBook(int searchBookId) {
       boolean search=true;
       for(Book book :bookList) {
           if(book.getBookId()==searchBookId) {
               System.out.println("Book Id:"+book.getBookId()+" Tittle :"+book.getTittle()+" Auhtor Surname:"+book.getAuthorSurname()+
                       " Author FirstName:"+book.getAuthorFirsName()+" Purchase Date:"+book.getPurchaseDate()+" staus:"+book.getStatus());
               search=false;
           }
          
       }
       if(search) {
           System.out.println(searchBookId+" Not exist");
       }
   }
   private static void searchStudent(int searchStudentId) {
       boolean search=true;
       for(Student st :studentList) {
           if(st.getStudentId()==searchStudentId) {
               System.out.println("Student Id:"+st.getStudentId()+" SurName:"+st.getSurname()+" lastName:"+st.getLastName()+
                       "Age:"+st.getAge()+" Sex:"+st.getSex());
               search=false;
           }
          
       }
      
       if(search) {
           System.out.println(searchStudentId+" Not exist");
       }
      
   }
   private static void displayAllBook() {
       for(Book book :bookList) {
           System.out.println("Book Id:"+book.getBookId()+" Tittle :"+book.getTittle()+" Auhtor Surname:"+book.getAuthorSurname()+
                   " Author FirstName:"+book.getAuthorFirsName()+" Purchase Date:"+book.getPurchaseDate()+" staus:"+book.getStatus());
       }
      
   }
   private static void displayAllStudent() {
       for(Student st :studentList) {
           System.out.println("Student Id:"+st.getStudentId()+" SurName:"+st.getSurname()+" lastName:"+st.getLastName()+
                   "Age:"+st.getAge()+" Sex:"+st.getSex());
       }
      
   }
   private static boolean checkBookId(int bookId) {
       for(Book book: bookList) {
           if(book.getBookId()==bookId) {
               return true;
           }
       }
       return false;
   }
   private static boolean checkStudentId(int id) {
       for(Student st :studentList) {
           if(st.getStudentId()==id) {
               return true;
           }
       }
       return false;
   }
      private static boolean checkFacultyId(int id) {
       for(Faculty st :FacultyList) {
           if(st.getFacultyId()==id) {
               return true;
           }
       }
       return false;}
      private static void displayAllFaculty() {
       for(Faculty st :FacultyList) {
           System.out.println("Faculty Id:"+st.getFacultyId()+" SurName:"+st.getSurname()+" lastName:"+st.getLastName()+
                   "Age:"+st.getAge()+" Sex:"+st.getSex());
       }
      
   }
         private static void searchFaculty(int searchFacultyId) {
       boolean search=true;
       for(Faculty st :FacultyList) {
           if(st.getFacultyId()==searchFacultyId) {
               System.out.println("Faculty Id:"+st.getFacultyId()+" SurName:"+st.getSurname()+" lastName:"+st.getLastName()+
                       "Age:"+st.getAge()+" Sex:"+st.getSex());
               search=false;
           }
          
       }
      
       if(search) {
           System.out.println(searchFacultyId+" Not exist");
       }
      
   }
   }

output is this
----------Please choose one of below Options----------
1. Add Student
2. Add Faculty
3. Add Book
4. Display All Students
5. Display All Faculty
6. Display All Books
7. Search Student
8. Search Faculty
9. Search Book
10. Borow Book
11. Display All Borow Book
12. Return Book
13. Exit
your choice: 2
Enter Faculty Id
33
Enter Faculty surName
Enter Faculty lastName
m
Enter Faculty Age
2
Enter Faculty Sex :

What I have tried:

student class:
public class Student  {
   public int studentId;
   public String surname;
   public String lastName;
   public int age;
   public String sex;
  
   public Student(int studentId, String surname, String lastName, int age, String sex) {
       this.studentId = studentId;
       this.surname = surname;
       this.lastName = lastName;
       this.age = age;
       this.sex = sex;
   }

   public int getStudentId() {
       return studentId;
   }

   public void setStudentId(int studentId) {
       this.studentId = studentId;
   }

   public String getSurname() {
       return surname;
   }

   public void setSurname(String surname) {
       this.surname = surname;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   public String getSex() {
       return sex;
   }

   public void setSex(String sex) {
       this.sex = sex;
   }
  
  

}

faculty class:
public class Faculty {
   public int FacultyId;
   public String surname1;
   public String lastName1;
   public int age1;
   public String sex1;
    private int Faculty;
  
   public Faculty(int FacultyId, String surname, String lastName, int age, String sex) {
      
       this.FacultyId = FacultyId;
       this.surname1 = surname;
       this.lastName1 = lastName;
       this.age1 = age;
       this.sex1 = sex;
   }

   public int getFacultyId() {
       return Faculty;
   }

   public void setFacultyId(int FacultyId) {
       this.Faculty = FacultyId;
   }

   public String getSurname() {
       return surname1;
   }

   public void setSurname(String surname) {
       this.surname1 = surname;
   }

   public String getLastName() {
       return lastName1;
   }

   public void setLastName(String lastName) {
       this.lastName1 = lastName;
   }

   public int getAge() {
       return age1;
   }

   public void setAge(int age) {
       this.age1 = age;
   }

   public String getSex() {
       return sex1;
   }

   public void setSex(String sex) {
       this.sex1 = sex;
   }
}

book class:
public class Book {
   public int bookId;
   public String tittle;
   public String authorSurname;
   public String authorFirsName;
   public String purchaseDate;
   public String status;
  
   public Book(int bookId, String tittle, String authorSurname, String authorFirsName, String purchaseDate,
           String status) {
      
       this.bookId = bookId;
       this.tittle = tittle;
       this.authorSurname = authorSurname;
       this.authorFirsName = authorFirsName;
       this.purchaseDate = purchaseDate;
       this.status = status;
   }

    Book(int bookId, String tittle, String purchaseDate, String status) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   public int getBookId() {
       return bookId;
   }

   public void setBookId(int bookId) {
       this.bookId = bookId;
   }

   public String getTittle() {
       return tittle;
   }

   public void setTittle(String tittle) {
       this.tittle = tittle;
   }

   public String getAuthorSurname() {
       return authorSurname;
   }

   public void setAuthorSurname(String authorSurname) {
       this.authorSurname = authorSurname;
   }

   public String getAuthorFirsName() {
       return authorFirsName;
   }

   public void setAuthorFirsName(String authorFirsName) {
       this.authorFirsName = authorFirsName;
   }

   public String getPurchaseDate() {
       return purchaseDate;
   }

   public void setPurchaseDate(String purchaseDate) {
       this.purchaseDate = purchaseDate;
   }

   public String getStatus() {
       return status;
   }

   public void setStatus(String status) {
       this.status = status;
   }
}
Posted
Updated 25-Apr-21 9:33am
Comments
Patrice T 25-Apr-21 15:27pm    
And you plan to tell us what is the problem N

1 solution

This is a classic bug. It's caused by switching between reading tokens with br.nextInt(), br.next(), and others and reading whole lines with br.nextLine().

Most of the Scanner methods read and convert one token. Since there may be multiple tokens on a single line, the Scanner reads input characters up to, but not including, the first character after the token. When reading from the console, that character is usually the '\n' newline character that ended the input line. If the next call is another token-oriented input, there's no issue since reading a token will automatically skip over spaces, tabs and newlines to get to the start of the next token. If the next method called is a .nextLine(), though, it will see that '\n' at the start of input and return an empty line.

That's why you see your surname prompt skipped. The end of the line with the facultyID was still in the scanner when you called br.nextLine() to read the surname.

The simplest fix is to insert an extra call to br.nextLine() after reading one or more tokens and before reading whole lines. The code for case 2 of your menu code could look like:

Java
case 2:
System.out.println("Enter Faculty Id");
int id1 = br.nextInt();
boolean chekId1=checkFacultyId(id1);
if(chekId1) {
    System.out.println("FacultyId Already Exist");
    break;
}

br.nextLine(); // <<----- all token input done, skip to start of new line

System.out.println("Enter Faculty surName");
String surname1= br.nextLine();
System.out.println("Enter Faculty lastName");
String lastName1= br.nextLine();
System.out.println("Enter Faculty Age");
int age1 = br.nextInt();
System.out.println("Enter Faculty Sex :");
String sex1= br.nextLine();
Faculty Faculty=new Faculty(id1, surname1, lastName1, age1, sex1);
FacultyList.add(Faculty);
break;


For some reason that isn't indenting. I'm new here. That has just one extra br.nextLine inserted. You'll have to do something like this each time you switch from reading tokens to reading lines. Don't put that extra call between existing .nextLine() calls, though. Each .nextLine() reads and discards the '\n' character than ended the line.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900