// Problem Statement: WAP to create a class Library and use methods addBook and showAvailableBooks to store and show books in the library. // I am new to java and I am getting an issue after running the below program. I am not able to fill the first index place of the array addBook.
package com.company;
import java.util.Scanner;
class Library{
Scanner sc = new Scanner(System.in);
int numOfBooks;
String[] addBook;
Library(){
System.out.print("Enter the number of books you want to add to the library: ");
numOfBooks = sc.nextInt();
this.addBook = new String[numOfBooks]; //New String called "addBook" is being created.
}
public String[]addBook(){
for(int i=0; i<numOfBooks; i++){
int j = i+1;
System.out.print("Add Book "+j+" Name: ");
this.addBook[i] = sc.nextLine();
}
return addBook;
}
public void showAvailableBooks(){
for(int i=0; i<numOfBooks; i++){
System.out.println(addBook[i]);
}
}
}
public class CWH_51_Exercise_4 {
public static void main(String[] args) {
Library l = new Library();
l.addBook();
l.showAvailableBooks();
}
}