We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,324 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Searching a string in object array using Java?

I have the user enter a string e.g. subject of book using a scanner then a for loop, to print out all the books with the same subject to what the user entered.

But I'm having a problem in that it will only print out the first occurrence.

For example if the user enters IT1 if will only print out Java for Beginners, whereas it should print Java for Beginners and OO Programming due to they are both attached to IT1.

Heres my code

b[0] = new Book("Java for Beginners");
b[1] = new Book("OO Programming");

s[0] = new Subject("IT1", "Java");
s[1] = new Subject("IT2", "Software Design");
s[2] = new Subject("IT3", "UML");

bk[0] = new BookSub(b[0], s[0], s[1]);
bk[1] = new BookSub(b[1], s[0], s[1]);


System.out.println("Enter the Book Subject: ");

search = scan.next();
int index = 1;
for (int i = 0; i < bk.length; i++) {

if (search.equalsIgnoreCase(m[i].getSubject… {
index = i;
System.out.print(s[i].getName()+" "); 
}
3
Contributors
3
Replies
16 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
newbie-java
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

how exactly do you want us to know anything from that code?
your comparing an m object without informing us what m is (is it a book, a subject, a penguin, ... )

also, informing us of what's in the book, booksub, subtitle class could help out

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 628
Skill Endorsements: 25

A gorilla, a donkey, a cup of tea, ME, stultuske? .....

One a serious note, yeah as stultuske said please post the entire code so that we could help you out.

stevanity
Posting Whiz
302 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
Skill Endorsements: 0

stevanity: you may want to check up on his other thread, he's cross-posted it.

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 628
Skill Endorsements: 25

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.3145 seconds using 2.76MB