0

So I've got this String with book information:

String data = "Harry Potter 1 | J.K. Rowling| 350 | Fantasy | Hunger Games | Suzanne Collins | 500 | Fantasy | The KingKiller Chronicles | Patrick Rothfuss | 400 | Heroic Fantasy" 

Then I split the String:

String splitData = data.split("\\|"); 

This will cause Harry Potter 1 to be in position 0, J.K. Rowling to be in position 1, 350 to be in position 2, etc.

You might see a pattern in here, which is the fact that at position 0 is a title of a book, at position 1 is the author, at position 2 is the amount of pages and at position 3 is the genre. Then it starts again at position 4, which is again the title of a book, position 5 being the Author of the book, etc etc. I assume that you understand where I'm going.

Now let's say that I want to display all those elements separately, like printing all the titles apart, all the authors, all the amount of pages, etc. How would I accomplish this?

This should be possible to do since the titles are in 0, 4, 8. The authors are in 1, 5, 9, etc.

5
  • 1
    Nope, if you look at my previous question, I'm trying to make a manga reader. This gets the title of the manga, the author, etc, etc. I'm trying to process this but I don't know how ^^. Commented Apr 9, 2012 at 12:32
  • try on your own first, if some problem occurs, then ask. Commented Apr 9, 2012 at 12:33
  • What have you tried? You already figured out the relation between array indexes and "types", you have all you need. Commented Apr 9, 2012 at 12:33
  • You have the pattern down, (titles in 0,4,8 ect.) Your pretty much done! Commented Apr 9, 2012 at 12:37
  • Yeah, just a bad day today, but I got the answer I needed already. Commented Apr 9, 2012 at 16:42

2 Answers 2

4
String data = "Harry Potter 1 | J.K. Rowling| 350 | Fantasy | Hunger Games | Suzanne Collins | 500 | Fantasy | The KingKiller Chronicles | Patrick Rothfuss | 400 | Heroic Fantasy"; 
    String[] splitData = data.split("\\|");
    for(int i=0; i<splitData.length;i++) {
        if(i % 4 == 0) System.out.println("Title: "+splitData[i]);
        else if(i % 4 == 1) System.out.println("Author: "+splitData[i]);
        else if(i % 4 == 2) System.out.println("Pages: "+splitData[i]);
        else if(i % 4 == 3) System.out.println("Genre: "+splitData[i]);
    }

Difficult, isnt it?

Sign up to request clarification or add additional context in comments.

2 Comments

I swear to [name of spiritual leader you believe in] that I would have known this any other day, but I'm having many troublesome thoughts today so my mind was clogged. Thanks though! Btw, 3 of those if-statements should be else-if statements... that makes the program slightly more efficient.
you can also use switch: i % 4 will be calculated only once
2

You can recall that for loop lets you perform any modifications in the last expression, not only i++. For this case, you can use i += 4. Then in each iteration the name will ne at splitData[i], the author at splitData[i+1], the number of pages at splitData[i+2], and the genre at splitData[i+3].

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.