2

I am creating a commercial software and got stuck in place, where I need to fill an Array with information from multiple JTextBox fields.

I created an Array -

 public String[][] BookAttributes = new String[BookRows][BookLines];

I want to save information added by User from following JTextFields in [BookLines] column for each book, which should auto-increment every time when new book is added to stock.

private JTextField tfBookName;
private JTextField tfBookCost;
private JTextField tfBookYearOfPublication;
private JTextField tfBookPublishingHouse;
private JTextField tfBookISBN;
private JTextField tfBookAuthor;
private JTextField tfBookNrOfPages;

Unfortunately, I cannot find a solution how to put this all together.

Any Help appreciated.

Thank you!

1 Answer 1

3

If I understood you correctly then you are looking to save the Book information like Name, Cost etc. to an array.

But, in the above case you are creating a 2-D String Array which looks something like this:

Location (0,0) of array -> "foo"
Location (0,1) of array -> "bar"
...
...

What you actually need is a class say, Book which can hold the information regarding the different attributes. Something like as follows:

public class Book {

    private String bookName;
    private String bookCost;
    private String bookYearOfPublication;
    private String bookPublishingHouse;
    private String bookISBN;
    private String bookAuthor;
    private String bookNrOfPages;

    /* Constructor, Getter, Setters */
    ...
}

Next you can create an array of this class like this:

int bookRows = 100;
Book[] booksInfo = new Book[bookRows];

And simply code a for loop for stroing the different book details as follows:

for(int i = 0; i < bookRows; i++) {
    Book book = new Book();

    book.setBookName(tfBookName);
    ...
    ...

    booksInfo[i] = book;
}

You can further override the toString() method of the Book class if you want to print the different attributes of any book by simply using System.out.println(...).

Here is the code snippet:

@Override
public String toString() {
    return new StringBuilder().append("BookName: ").append(bookName)
                              .append(" | Book Cost: ").append(bookCost).toString();
}
Sign up to request clarification or add additional context in comments.

8 Comments

@Helvijs What is the problem that you are facing?
I tried this, but book.setBookName(tfBookName.getText()); brings me an error, that I cannot make a static reference tothe a non-static method from the type Book ! So Eclipse want me to make setBookName method static (which I don't think need to be static, because it's setter). @user2004685
@Helvijs Are you doing everything inside the main() function?
No. I already checked, I don't have any static variables or methods involved in this process. I created class Book with setters and getters for all attributes needed. And in different class I am using Book class Object to store all this information in it
I found the problem, everything works now. Thank you!
|

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.