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

Book[] b = new Book[2];

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

Subject[] s = new Subject[3];

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

BookSub[] bk = new BookSub[2];

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(bk[i].getSubject… {
index = i;
System.out.print(s[i].getName()+" "); 
}

Book Class

private String name;

public Book(String n) {
this.name = n;

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Subject Class

public Subject (String sc, String sd){

subjectcode = sc;
subjectdesc =sd;
}

public String getSubjectcode() {
return subjectcode;
}

public void setSubjectcode(String subjectc) {
this.subjectcode = subjectcode;
}

public String getSubjectdesc() {
return subjectdesc;
}

public void setSubjectdesc(String subjectdesc) {
this.subjectdesc = subjectdesc;
}


@Override
public String toString() {

return subjectcode +" "+ subjectdesc;
}
}

BookSub Class

private Book book;
private Subject subject;
private Subject subject1;

public BookSub (Book b, Subject s, Subject s1){
book = b;
subject = s;
subject = s1;
    }

    public Subject getSubject() {
        return module;
    }

    public Subject getsubject1() {
        return subject1;
    }

    public void setSubject(Subject subject1) {
        this.subject1 = subject1;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    public Book getBook() {
        return this.book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

  
}

1. don't start several threads for one problem
2. try and compile your code, you'll no doubt get some very clear error messages that'll tell you you are doing some things quite wrong, like having two methods with exactly the same name.
3. also: realize that getSubject… is not a correct way to call a method.
4. check what type of object you're storing in bk and don't use variables that you don't declare.

1. I wouldn't have started a new thread if it had allowed me to edit my previous thread.
2. I have complied the code and there are no errors.
3. Im new to java, so how would you suggest to call the method?
4. In bk it is referenced back to book and subject, where is the undeclared variable?

1. ehm,normally you can, and even if not, you could have posted your code in a new post in the other thread

2.
BookSub (as you posted it) will not compile because:
a. your constructor is trying to return an undefined variable module
b. you have two methods called getSubject, rename the appropriate one to getSubject1
Subject, as you posted it, will not compile, since you do not declare subjectcode or subjectdesc, but I assume you just didn't post the entire class
your main method will not compile because:
a. you are using a search variable, which you don't declare, or at least not in the code you've provided
b. you are calling a getSubject... method, while the correct way to call a method is getSubject()
c. the next line

System.out.print(s[i].getName()+" ");

will not compile, since s refers to an instance of Subject, which does not have a variable name, or a getName() method.
c. you can not use scan.next unless you actually have a scan variable on which you can call the method. so, I assume (yet again) that somewhere you create an instance of the Scanner class, but, it is not in the code you've shown.

3. yes, I notice, but this is the very basis of OO design, using a method, getter, setter ...
since you manage to do it correctly in the other places you do it, why exactly do you not know how to do it for this particular one?

4. see my above list for which variables and which class to find them in

Ok have now made the required adjustments so the code compiles.

Now when I enter IT1 it will print out Java for Beginners whereas it should be printing Java for Beginners along with OO Programming?

Driver Class

public static void main(String[] args) {
        
        String search;

        Scanner scan = new Scanner(System.in);


        Book[] b = new Book[2];

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

        Subject[] s = new Subject[3];

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

        BookSub[] bk = new BookSub[2];

        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(s[i].getSubjectcode())) {
                index = i;
                System.out.print(b[i].getName() + " ");
            }



        }
    }
}

Book Class

public class Book {
   
    private String name;

    public Book(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

Subject Class

public class Subject {

    private String subjectcode, subjectdesc;

    public Subject(String sc, String sd) {

        subjectcode = sc;
        subjectdesc = sd;
    }

    public String getSubjectcode() {
        return subjectcode;
    }

    public void setSubjectcode(String subjectc) {
        this.subjectcode = subjectcode;
    }

    public String getSubjectdesc() {
        return subjectdesc;
    }

    public void setSubjectdesc(String subjectdesc) {
        this.subjectdesc = subjectdesc;
    }

    @Override
    public String toString() {

        return subjectcode + " " + subjectdesc;
    }
}

BookSub Class

public class BookSub {

    private Book book;
    private Subject subject;
    private Subject subject1;
    

    public BookSub(Book b, Subject s, Subject s1) {
        book = b;
        subject = s;
        subject = s1;
    }

    public Subject getSubject() {
        return subject;
    }

    public Subject getsubject1() {
        return subject1;
    }

    public void setSubject(Subject subject1) {
        this.subject = subject;
    }

    public void setSubject1(Subject subject1) {
        this.subject1 = subject1;
    }

    public Book getBook() {
        return this.book;
    }

    public void setBook(Book book) {
        this.book = book;
    }
}

nope, it's printing exactly what you are telling it to print.

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

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

on your search, the index of IT1 will return 0, and then you print the value stored in b[0] which is:
Java for beginners

also, you may want to watch out a bit. what do you think will happen if you try the IT3 subjectcode?

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

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

            if (search.equalsIgnoreCase(s[i].getSubjectcode())) {
                index = i;
                System.out.print(b[i].getName() + " ");
            }
         if (search.equalsIgnoreCase(s[i].getSubjectcode())) {
                index = i++;
                System.out.print(b[i].getName() + " ");
        }

I have tried to enter the if statement twice in order to return all books. So would I need an if statement for each now book for example if i had 5 books i would have 5 if statements? :confused:

If I enter IT3 it will not return anything due to IT3 not being attached to any Books.

What do u mean it will not return anything? Actually IT3 is index "2". and do u have a b[2]?? {u have only b[0] and b[1] } so ull get an exception (ArrayIndexOutOfBoundsException)

As stultuske says it is doing exactly what u want it to do.

You are not making use of the Booksub class. Think of a solution incorporating that class as well. Then you can easily get a solution.

And the above code of yours prints the first book matching the INDEX of the matched subject and the next book.

But do u connect books and subjects based on INDEX??? No right? you map them with the Booksub class.

So think of a solution using that class.

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.