0

I am trying to Creating a library management system which is capable of issuing books to the students.
Book should have info like:
1. Book name
2. Book Author
3. Issued to
4. Issued on
User should be able to add books, return issued books, issue books

When I am executing the below code and choosing option 1 as choice I am not able to give input to the bName String and author String

am I doing something wrong, I am new to java please help me

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

/*
Creating a library management system which is capable of issuing books to the students.
Book should have info like:
1. Book name
2. Book Author
3. Issued to
4. Issued on
User should be able to add books, return issued books, issue books
 */
class BookInfo
{
    protected String name,author,issuedTo,issuedOn;
    public BookInfo(String bName,String bAuthor) {
        this.addBook(bName,bAuthor);
    }
    public String getBookName()
    {
        return name;
    }
    public String getAuthor()
    {
        return author;
    }
    public String getIssuedOn()
    {
        return issuedOn;
    }
    public String getIssuedTo()
    {
        return issuedTo;
    }
    public void addBook(String name,String author)
    {
       this.name=name;
       this.author=author;
    }
    public void issueBook(String name)
    {
        this.issuedTo=name;
        LocalDateTime t=LocalDateTime.now();
        DateTimeFormatter dTF=DateTimeFormatter.ofPattern("dd/MMM/yyyy       HH:mm:ss");
        this.issuedOn=t.format(dTF);
    }
  public void returnIssuedBooks()
  {
      issuedTo=null;
      issuedOn=null;
  }

}

public class Main {

    public static void main(String[] args){

        ArrayList<BookInfo> l1 = new ArrayList<>();
        int choice;
        String bName;
        String name;
        String author;
        boolean isTrue = true;
        Scanner obj = new Scanner(System.in);
        System.out.println("welcome to the Library Management System");
            while (isTrue) {
                System.out.println("choose from the options below :");
                System.out.println("press 1 for adding the book");
                System.out.println("press 2 for issuing the book");
                System.out.println("press 3 for returning the issued book");
                System.out.println("press 4 to exit");
                choice = obj.nextInt();
                try {
                    switch (choice) {
                        case 1:
                            System.out.println("enter the name of the book :");
                            bName = obj.nextLine();
                            System.out.println("enter the author of the book");
                            author = obj.nextLine();
                            l1.add(new BookInfo(bName,author));
                            break;

                        case 2:
                            System.out.println("enter the name of the book to issue");
                            bName = obj.nextLine();
                            System.out.println("enter your name :");
                            name = obj.nextLine();
                            for (BookInfo obj2 : l1) {
                                if (obj2.getBookName().equals(bName)) {
                                    obj2.issueBook(name);
                                }
                            }
                            break;

                        case 3:
                            System.out.println("enter the name of the book you want to return");
                            bName = obj.nextLine();
                            for (BookInfo obj2 : l1) {
                                if (obj2.getBookName().equals(bName)) {
                                    obj2.returnIssuedBooks();
                                }
                            }
                            break;

                        case 4:
                            isTrue = false;
                            break;

                        default:
                            throw new Exception();
                    }
                }catch(Exception e)
                {
                    System.out.println("enter the correct input");
                }
            }
    }
    }

this is the following output

welcome to the Library Management System
    choose from the options below :
    press 1 for adding the book
    press 2 for issuing the book
    press 3 for returning the issued book
    press 4 to exit
    1
    enter the name of the book :
    enter the author of the book 
firstuser
  • 5
  • 4

1 Answers1

0

That's because of the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter" and so the call to Scanner.nextLine returns after reading that newline. There are two options to resolve this issue,

1. Read the input through Scanner.nextLine and convert your input to the proper format you need

    Scanner obj = new Scanner(System.in);
    int choice = Integer.parseInt(obj .nextLine());
    

2. Add Scanner.nextLine call after each Scanner.nextInt

    Scanner obj = new Scanner(System.in);
    int choice = = obj.nextInt();
    obj.nextLine();
Pramod H G
  • 916
  • 8
  • 12