1

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];
        }
    }
}
3
  • you cannot use sname which of type String as index of array. if sname is int value in String, use Integer.parseInt Commented Jun 15, 2020 at 14:31
  • brother, how can I convert a book name to an integer? I want to input a book name and price in a two-dimensional array. so the name should be string and price should be int, double, or float. Commented Jun 15, 2020 at 14:42
  • This sounds like a place where Java.util.Dictionary Class would be useful. The developer here might consider implementing a key (some unique integer or unique string, perhaps) so that you can collect the data more easily in a Dictionary, which consists of Key-Value pairs. Commented Jun 15, 2020 at 21:46

2 Answers 2

1

You never declared the array song, maybe you wanted to write

bk[i][j] = ...

Now you want to create a new "Book" for every sname and sprice that you read, so you have two options:

1) In each iteration create a new empty Book

Book tmp = new Book();

then you set his Name and his Price

tmp.setName(sname);
tmp.setPrice(sprice);

and then you assign the new Book to the current element of bk

bk[i][j] = tmp;

or

2) Add a constructor to the class Book that has Name and Price as parameters

public Book(String n, int p){
    name = n;
    price = p;
}

and use it to instantly create a new Book and assign it to the current element of bk

bk[i][j] = new Book(sname, sprice);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer. but what about bk[][] variable types?
@Almamun each element in bk is a Book, so it contains a name and a price
yes, I got that. but that way it doesn't work. then I define one-dimensional array Book bk[] = new Book[l]; and used only one for loop and then just set the value using the constructor. bk[i]= new Book(name,price);
1

So what you require is Book[String name][int price]. That is not how 2D arrays work.

While declaring:

int l = 5;
Book[][] bk = new Book[l][l];

You are implementing a 2D Book array that can have 25 book records. 1D array of Books is sufficient for your requirements.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int l = 25;//any size you can have
    Book[] bk = new Book[l];
    for (int i = 0; i < l; i++) {
        System.out.println("Enter Song: ");
        String sname = in.next();
        System.out.println("Enter price: ");
        int sprice = in.nextInt();
        Book book = new Book();
        book.setName(sname);
        book.setprice(sprice);
        bk[i] = book;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.