-1

Given this class:

public class Book {
    private String bookName;
    private String[] chapters = new String[9];
    private int numberOfChapters;

    public Book(String bn){
        bookName = bn;
    }
    public Book(String b[]){
        chapters = b;
    }
    public void addChapter(String chapter){
        chapters[numberOfChapters] = chapter;
        numberOfChapters++;
    }
    public String[] getChapter(){
        return chapters;
    }
    public int getNumberOfChapters(){
        return numberOfChapters;
    }
    public String getBookName(){
        return bookName;
    }
    public String toString(){
        return("Book Name: " + getBookName() + " chapters[] = " + getChapter() + " number of chapters :  " + getNumberOfChapters());
    }

}

I need to write a driver that creates an array of chapters (strings), then assigns each object within it to the Book object. My problem is that when I do this, the toString() method doesn't print the chapters properly, and instead returns the memory address.

Here's what I have so far:

public class BookTest {

    public static void main(String[] args) {
        String[] BookArray = new String[9];
        for(int i = 0; i<BookArray.length; i++){
            BookArray[i] = new String("Chap"+(i+1));
        }
        Book b1 = new Book("Name1");
        for(int i=0; i<BookArray.length; i++){
            b1.addChapter(BookArray[i]);
        }
        System.out.println(b1.toString());
    }
}

Which prints

Book Name: Name1 chapters[] = [Ljava.lang.String;@2a139a55 number of chapters :  9

What do I need to change in my driver to properly print the chapters, without changing the toString() method?

2
  • Use a List<String> rather than a String[]. Commented Dec 5, 2015 at 16:52
  • Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a CC BY-SA license, for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: How does deleting work? …. If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. Commented Nov 16, 2019 at 16:26

5 Answers 5

2

Use Arrays.toString(getChapter()) in your toString method. I would rather use a List<String> instead of a String array, it's easier to add and remove chapters from the book. Or maybe a Map<int, String>, that way you can add chapters with both an ID (e.g. chapter 12) and a name for each chapter.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to get it working without modifying the class?
@agsl well no, not without changing the toString method of Book. When you print an Array, you print the memory address, not the contents of the Array.
0

Use the function

Arrays.toString(chapters)

to get a nice output.

1 Comment

Provide some kind of explanation too to support your answer.
0

Array is Object in Java.

the Java Language Specification writes:

In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

Hence Ljava.lang.String;@2a139a55 is printed.

Refer to Is it possible in Java to override 'toString' for an Objects array?

Comments

0

The function getChapters() returns the memory address, doesn't print the array.

To do so, first :

 import java.util.Arrays;

and then:

use the toString() method of Arrays class in order to return the string format of the array in your output.

 public String toString(){
    return("Book Name: " + getBookName() + 
           " chapters[] = " + Arrays.toString(getChapter()) +
           " number of chapters :  " + getNumberOfChapters());
 }

Comments

0

This can be achieved in many ways...

1)

Arrays.toString(chapters)

to get a nice output.

2)

You can also use:-

Arrays.asList(getChapter())

3)Instead of String[] use List...in this way...automatically toString will print it in correct format.

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.