• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to return an Array of Strings  RSS feed

 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I have the following method public String[] getBookTitlesContaining(String wordInTitles) that has to return an Array of Strings[] with the titles of all the books that contain wordInTitle in them. I wrote this:


..but unfortunately I get an error. Any advice? Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps showing us the error could be helpful?
 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error:

java.lang.ArrayStoreException
null (in java.lang.System)
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always tell us the exact details of the error. If we know that, we can have a better idea about the solution.

If you had been taught about Streams, you could have achieved that much more easily. Anyway, let's look at what you are doing.
You appear to have a List of books.
You appear to be passing a word to each of the books in turn.
You appear to keep those books which contain that word.

That lot seems all right so far. Add a line immediately after the end of the loop like this:-
System.out.println(bookTitlesContaining);
Comment out lines 9 10 11 12 and write
return new String[]{"Moby Dick"}; (which you will remove in a minute or two) at the end of the method (otherwise it won't compile).
That ought to print a pair of square brackets [] with a list of book titles in between. That will let you see whether you have captured the books containing the word.

Then see how you can get an array out of that List instead of Moby Dick. Go through the methods of the List interface and see whether any of them will give you an array.
Beware: You say you want a String[] and my technique may give you a Book[]. You may have to add the title to bookListContaining instead of the book. In which case you will have to declare that List as List<String>. Then there is no risk of returning an array of books.

Note: This is not a complete answer. You will have to work out how to complete the solution for yourself.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think you are much closer to what I was suggesting than I originally thought.
You can get an array returned by simply writing
return bookTitlesMatching.toArray(matches);
…but that must be the last line in the method, and you still need to change things so your second List contains Strings.

Beware of multiple dots. There is something wrong with
if (inventory[i].getTitle().contains(word))...
Change your book class so it has a method like
public boolean titleContainsText(String text)...
That will look much better. Google for “Law of Demeter” to find out more.

Would this work for Java8 users?
 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so first of all thanks for the replies. I'm a little bit confused right now. I thought that the easiest way to return an Array of String was to declare and return a local String[] object. Perhaps I could use the following(see below), but unfortunately it doesn't compile. ("incompatible types: Books cannot be converted to java.lang.String - line 12

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About two hours ago, I wrote: . . . Would this work for Java8 users? . . . .
It wouldn't, but my attempt was not that far out. This did work:-The stream method turns the Book[] array into a Stream<Book>.
The map method takes the book and calls it getTitle method, so it creates a new Stream<String>.
The filter method checks whether the particular word is contained in the title; since we have a Stream<String> we can call methods of the String class.
The toArray method takes no arguments in which case it returns an Object[], or it returns a String[] if you pass an invocation of the String[] constructor using the membership operator :: and the keyword new.
I am pretty sure I could have passed Book::getTitle as an argument to map instead.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the version with two loops is an improvement on the previous version, I am afraid.
You need to check carefully whether you are putting Book objects or String objects into the arrays. Look at line 12, where I think the problem is.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little bit confused right now. I thought that the easiest way to return an Array of String was to declare and return a local String[] object.


This is true, but do you want to return a string array or a book array? If a book array, return Book[]; if a string array, find some way to represent a book as a string by overriding toString().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!