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.

Recommended Answers

All 2 Replies

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.

Thanks a lot, easier than anticipated :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.