I want to implement a two-dimensional array using user input. I have a Book class. It has two variables: int price and String name. I want to store 5 books information in a two-dimensional array. Book class code is below:
public class Book {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
Main Class code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int l = 5;
Book[][] bk = new Book[l][l];
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
// here i want to take user input.
System.out.println("Enter Song: ");
String sname = in.next();
System.out.println("Enter price: ");
int sprice = in.nextInt();
// in this line i am getting type
// error int can't convert to string
song[i][j] = song[sname][price];
}
}
}
snamewhich of typeStringas index of array. ifsnameis int value in String, useInteger.parseInt