1

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;
}

}
1
  • try the authors.split function, it takes a regex, have a look at other methods available in String, it helps knowing them Commented Oct 24, 2013 at 9:50

5 Answers 5

5

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

as Loic answered a few seconds ago, ArrayList books = Arrays.asList(strgArray); is cleaner and covers all senarios. so use that
changed my answer too so if anyone uses my part of code, then to use the correct snippet without exceptions. @yilmazburk good work once again :)
1

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;
}

Comments

1

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;
}

3 Comments

my code was very similar to urs, but it gave me an error." Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *"
try to add \\ in your regexp.
aaa yes... we forgot the \\ to distinguish the special character in our answers. @yilmazburk is correct
0

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.

Comments

0

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.

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.