Program Name: MyBookList.java [20]
You are being asked to write a program which will keep the list of books. The program will keep record for
the book’s title, price, and pages. You are required to use the two-dimensional String
array for this purpose.
The array would have n rows and 3 columns where n is the size entered by user. The first, second,
and third columns would be used for storing a book’s title, price and pages respectively. We will also treat
price and pages as a String. This array should be of this form:
Introduction to AI 1200.00 450
Reinforcement Learning 1300.99 1000
Deep Learning: Practical 1575.60 850
import java.util.*;
public class MyBookList {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter the number of books: ");
int n = keyBoard.nextInt();
String books[][] = new String[n][3];
keyBoard.nextLine();
for (int i = 0; i < n; i++) {
System.out.print("Enter the inforamtion for book " + (i + 1) + ":\n");
System.out.print("Enter book's title: ");
books[i][0] = keyBoard.nextLine();
System.out.print("Enter book's price: ");
books[i][1] = keyBoard.nextLine();
System.out.print("Enter book's pages: ");
books[i][2] = keyBoard.nextLine();
}
System.out.printf("%-20s%-20s%-20s\n", "Title", "Price", "Pages");
for (int i = 0; i < n; i++) {
System.out.printf("%-20s%-20s%-20s\n", books[i][0], books[i][1], books[i][2]);
}
keyBoard.close();
}
}
Comments
Leave a comment