0

Hi everyone, I'm making a program to store library books details, my constructor is such:

public Book(String author, String title, String publisher, int year)

and I'm using an array to store and number the books and details, as such:

for(int i = 0; i <= 100; i++)
{
	String book[i] = author + "-" + title + "-" + publisher + "-" + year;
}

The problem is, later on I need to print out the seperate details, each on different lines ie:

Judith Bishop
Java Gently
Addison-Wesley, 2001
Library number 345

so I wish to split the string at each "-" and put them on a new line. The question is how do I go about splitting the strings? I know there is a split method but I'm not sure how to use it.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by cherryduck
0

The question is how do I go about splitting the strings? I know there is a split method but I'm not sure how to use it.

Its as simple as :-

String[] splitString = book[i].split("-");

Look here for more details.

But beware of the "-" in "Addision-Wesley", even that "-" will be used for splitting, So either you will need disallow the user from using "-" in the original Strings or you will need to manually replace the "-" with some other marker when you concatenate them and then replace that marker with "-" when you want to again display it later after applying the split() method.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.