Bopeng Liu
Bopeng Liu

Reputation: 13

return an ArrayList method

This is a drive method for two other classes. which i posted here https://codereview.stackexchange.com/questions/33148/book-program-with-arraylist

I need some help for the private static ArrayList getAuthors(String authors) method. I am kind a beginner. so please help me finish this drive method. or give me some directions.

Instruction

some of the elements of the allAuthors array contain asterisks “*” between two authors names. The getAuthors method uses this asterisk as a delimiter between names to store them separately in the returned ArrayList of Strings.

import java.util.ArrayList;

public class LibraryDrive {

public static void main(String[] args) {

    String[] titles = { "The Hobbit", "Acer Dumpling", "A Christmas Carol",
            "Marley and Me", "Building Java Programs",
    "Java, How to Program" };

    String[] allAuthors = { "Tolkien, J.R.", "Doofus, Robert",
            "Dickens, Charles", "Remember, SomeoneIdont",
            "Reges, Stuart*Stepp, Marty", "Deitel, Paul*Deitel, Harvery" };

    ArrayList<String> authors = new ArrayList<String>();
    ArrayList<Book> books = new ArrayList<Book>();

    for (int i = 0; i < titles.length; i++) {
        authors = getAuthors(allAuthors[i]);
        Book b = new Book(titles[i], authors);
        books.add(b);
        authors.remove(0);
    }
    Library lib = new Library(books);
    System.out.println(lib);
    lib.sort();
    System.out.println(lib);

}

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
            // need help here.
    return books;
}

}

Upvotes: 1

Views: 94802

Answers (5)

yilmazburk
yilmazburk

Reputation: 917

Try this one but actually i do not understant why you remove zero indexed element of ArrayList in for loop.

private static ArrayList<String> getAuthors(String authors) {
    ArrayList<String> array = new ArrayList<String>();
    String[] authorsArray = authors.split("\\*");
    for(String names : authorsArray );
        array.add(names);
    return array;
}

Upvotes: 1

Apostolos
Apostolos

Reputation: 10463

try this

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
      String[] splitStr = authors.split("\\*");
      for (int i=0;i<splitStr.length;i++) {
        books.add(splitStr[i]);
       }
    return books;
}

Upvotes: 5

Lo&#239;c
Lo&#239;c

Reputation: 591

What I would suggest is to use String.split like here (but keep in mind that this method uses a regex as parameter):

private static ArrayList<String> getAuthors(String authors) {
    ArrayList books = new ArrayList<String>();
    String[] strgArray = authors.split("\\*"); 
    books.addAll(Arrays.asList(strgArray));
    return books;
}

or

private static ArrayList<String> getAuthors(String authors) {
    String[] strgArray = authors.split("\\*"); 
    ArrayList books = Arrays.asList(strgArray);
    return books;
}

Upvotes: 1

Rahul
Rahul

Reputation: 45060

Here's how you can go about doing it.

  1. Split the authors string you're getting as the method param based on the asterisk symbol. (Using String.split(delim) method)
  2. The resultant string[] array needs to be iterated over using a for loop and each iterated element should be added to your list. (Using List.add(elem) method)
  3. Once done, return that list(you are already doin that).

Now that you know how to do it, you need to implement the code by yourself.

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27496

Take a look at String#split method, this will help you to separate authors by asterisk. This method returns an array so you will need to check how many authors are in this array and then store each of them into the ArrayList.

Upvotes: 0

Related Questions