import java.util.Scanner;
public class BookRentalSystem {
    public static void main(String args[]) {

        //Declaration of variable and instances
        String bTitle, bType, bAuthor, bISBN, bStatus, search;
        int choice = 1;
        int bCount = 0;
        boolean found;
        Scanner input = new Scanner(System.in);
        Book[] books = new Book[5];

        //Executing user's selection from menu. Exit on 0
        while ( choice != 0){

            //Menu interface
            System.out.println( "============= M e n u =============" );
            System.out.println( "1. Add a new book" );
            System.out.println( "2. Book loan" );
            System.out.println( "3. Book return ");
            System.out.println( "4. Search book" );
            System.out.println( "0. EXIT");
            System.out.print( "Enter selection: ");
            choice = input.nextInt();

            switch (choice) {
            //Add new book
            case 1:
                System.out.print("Enter book title : ");
                bTitle = input.nextLine();

                System.out.print("Enter book type ( NA / A) : ");
                bType = input.nextLine();

                System.out.print("Enter book author : ");
                bAuthor = input.nextLine();

                System.out.print("Enter book ISBN : ");
                bISBN = input.nextLine();

                bStatus = "Available";
                System.out.println("Book added in the system..");

                books[bCount] = new Book(bTitle,bType,bAuthor,bISBN,bStatus);
                books[bCount].getDetail();
                bCount ++;
                break;

            //Error msg
            default :
                    System.out.println("Invalid selection");
            }
        }
    }
}

My problem is when after I input an int in 'choice', when I want to enter a string with space in bTitle say "Purpose Driven Life", the input skips and goes on the next input instead.
When I use input.next(); there's no problem. Only when I tried to use nextLine(), is there anyway to resolve this? I'll want the input to be able to assign a string with space

Recommended Answers

All 2 Replies

Thanks for pointing the right direction! =)

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.