Results 1 to 3 of 3
  1. #1
    java-newbie is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Searching a string in object array? For loop problem?

    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(bk[i].getSubjec… {
    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;
    subject1 = s1;

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Searching a string in object array? For loop problem?

    Your code is missing lots of bits. You should post code that compiles and executes if you want help.

    When you post code, please wrap it in code tags to preserve its formatting.

  3. #3
    java-newbie is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Searching a string in object array? For loop problem?

    Code is now executable.
    Same problem though?

    Driver Class
    Java Code:
    public class UI {
        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

    Java Code:
    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

    Java Code:
    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

    Java Code:
    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;
        }
    }

Similar Threads

  1. String array and for loop graphic
    By djtoxicdance in forum New To Java
    Replies: 6
    Last Post: 05-12-2011, 09:41 PM
  2. Refer to an object from an array/for loop
    By Chro in forum New To Java
    Replies: 2
    Last Post: 11-09-2010, 06:12 PM
  3. Replies: 4
    Last Post: 07-27-2010, 08:52 PM
  4. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 09:12 AM
  5. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •