9 Replies - 1854 Views - Last Post: 29 September 2012 - 03:22 AM Rate Topic: -----

#1 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Searching and display with getMethods not working

Posted 23 September 2012 - 12:17 AM

Okay, so the main problem I'm getting is that when I try to run a search for certain things within the array, it doesn't seem to find anything (at all) and goes straight to the error message at the end of the search loop. I think I'm supposed to use the getMethods in the constructor, but I haven't the slightest idea how to doe this...I assume I'm missing something, but I can't find/work out what it is.

Here is the constructor code:
public class Book{
    private String title;
    private String author;
    private String genre;
    private int year;
    
    //constructor
    public Book(String title, String author, String genre, int year){
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.year = year;
    }
    
    public String getTitle(){return title;}
    public String getAuthor(){return author;}
    public String getGenre(){return genre;}
    public int getYear(){return year;}
    
    public String toString(){
        return "\nTitle: " + title + "\nAuthor: " + author + "\nGenre: " + genre +"\nRelease Year: " + year + "\n";
    }
}


And here's the main method (in which the problem lies):
import java.util.*;
import java.io.*;

public class Library{
    public static void main(String[] args){
        //global declarations
        Book[] bookar = new Book[50];
        int bookcounter = 0;
        boolean flag = true;
        Scanner input = new Scanner(System.in);
        
        
        
        
        while(flag){
            System.out.println("\nPlease select the required task");
            System.out.println("\t1 - Add a Book\n \t2 - List all Books\n" +
                               "\t3 - Search for Book by Title\n" +
                               "\t4 - Search for Book by Author\n" +
                               "\t5 - Search for Book by Genre\n" +
                               "\t6 - Search for book by Year\n " +
                               "\t7 - Quit");
             int menuchoice;
             menuchoice = input.nextInt();
    switch(menuchoice){   
        case 1: //Add a book
        //Information input for book
        Scanner input1 = new Scanner(System.in);
        System.out.println("\nTitle please");
        String title = input1.nextLine();
        System.out.println("\nAuthor please");
        String author = input1.nextLine();
        System.out.println("\nGenre please");
        String genre = input1.nextLine();
        System.out.println("\nYear please");
        int year = input1.nextInt();
                    
        //create the book
        Book b = new Book(title, author, genre, year);
                    
        //add book to array
        bookar[bookcounter] = b;
        bookcounter++;
        break;
        
        case 2: //list all books
        for(int c=0; c<bookcounter; c++){
            System.out.println("\n==============================\n" + bookar[c]);
        }
        System.out.println("\n==============================\n");
        break;
        
        case 3: //search for book by Title
        {
            int searchCount;
            Scanner titleSearch = new Scanner(System.in);
            boolean found = false;
            System.out.println("Please enter the Title of the book you wish to search");
            String[] findTitle = new String[1];
            findTitle[0] = titleSearch.next();
            
            //Search Book array
            for(searchCount = 0; searchCount>bookcounter; searchCount++){
                if(bookar[searchCount].equals(findTitle[0])){
                    System.out.println("\n\n===========================\n" + bookar[searchCount] + "\n");
                    found = true;
                }//if
            }//for
            //error message if no books found
            if(!found){
                System.out.println("\nNothing Found\n\n");
            }//if
        }//case3
        break;
        
        case 4: //search for book by Author
        {
            int searchCount;
            Scanner authorSearch = new Scanner(System.in);
            boolean found = false;
            System.out.println("Please enter the Author of the books you wish to search for");
            String[] findAuthor = new String[1];
            findAuthor[0] = authorSearch.next();
            
            //Search Book array
            for(searchCount = 0; searchCount>bookcounter; searchCount++){
                if(bookar[searchCount].equals(findAuthor[0])){
                    System.out.println("\n\n===========================\n" + bookar[searchCount] + "\n");
                    found = true;
                }//if
            }//for
            //error message if no books found
            if(!found){
                System.out.println("\nNothing Found\n\n");
            }//if
        }//case4
        break;
        
        case 5: //search for book by Genre
        break;
        
        case 6: //search for book by Year
        break;
        
        case 7:
            flag = false;
        break;
        
        default:
        System.out.println("\n===============\nIncorrect input\n" +
                           "===============\n");
        break;
    }//switch
}//main
}
}



Any suggestions or help would be much appreciated.
Regards, kosmro.

Is This A Good Question/Topic? 0
  • +

Replies To: Searching and display with getMethods not working

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Searching and display with getMethods not working

Posted 23 September 2012 - 12:36 AM

How about an error message so we can avoid going over everything line by line?
Was This Post Helpful? 0
  • +
  • -

#3 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 23 September 2012 - 12:46 AM

There's no error code, it just doesn't display anything.

I'll give example. After entering book details under case1 (line 26), I use case 3 (line 53-74) to search the array (with another array) for a Title match, but regardless of how I enter what I want to look for, it just goes to the 'nothing found' message (line 70-72).
The information is sitting in the array, the 'display all' - case 2 (line 46-51) confirms that.

I can't get it to single out a full array entry (or multiple if there is more than one) when I search for it.


Does that clarify anything?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Searching and display with getMethods not working

Posted 23 September 2012 - 01:16 AM

Just quickly looking it over, I see this in your 3rd switch:

if(bookar[searchCount].equals(findTitle[0]))

.

Your findTitle[0] is a String object. bookar[searchCount] is a book object. It can never work, :)

If you were searching by title you need to get the book object from the array, call its getTitle(), then check that against your findTitle[0] String.

Also, your code needs more thought, as well as a complete redesign. It's too complicated. :) Switching to ArrayList is a good start.

This post has been edited by farrell2k: 23 September 2012 - 01:18 AM

Was This Post Helpful? 1
  • +
  • -

#5 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 23 September 2012 - 01:27 AM

Ah, I see. I shall give that a shot.

And yes, I know the code is a bit overly complex and inefficient, it's just a draft at the moment, once I get it fully functional, I shall start simplifying it.

Thanks farrell2k
Was This Post Helpful? 0
  • +
  • -

#6 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 28 September 2012 - 10:58 PM

Okay, so I have had a good go at redesigning it (I really only put in ArrayLists instead of a limited Array). But every time I try to compile, I get the following error:
"Compiler Warning
Warnings from last compilation
Note: ~/Library2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."

Then after I close the error window, down the bottom it shows the following "array required, but java.util.ArrayList found" and '[scount]' is highlighted on line 67.

I am using BlueJ 3.0.7 with Java 1.6.0_35 (Macintosh OS X 10.8.2)

The rebuilt Library file has been renamed as Library2.java (as I left Library.java with Arrays to refer to later or to test things later if I needed).
import java.util.*;
import java.io.*;

public class Library2{   
    public static void main(String[] args){
        ArrayList bookar = new ArrayList();
        ArrayList searchar = new ArrayList();
        //global declarations
        int bookcounter = 0;
        boolean flag = true;
        Scanner input = new Scanner(System.in);
        
        
        
        
        while(flag){
            System.out.println("\nPlease select the required task");
            System.out.println("\t1 - Add a Book\n \t2 - List all Books\n" +
                               "\t3 - Search for Book by Title\n" +
                               "\t4 - Search for Book by Author\n" +
                               "\t5 - Search for Book by Genre\n" +
                               "\t6 - Search for book by Year\n " +
                               "\t7 - Quit");
             int menuchoice;
             menuchoice = input.nextInt();
             switch(menuchoice){   
                 case 1: //Add a book
                    //Information input for book
                    Scanner input1 = new Scanner(System.in);
                    System.out.println("\nTitle please");
                    String title = input1.nextLine();
                    System.out.println("\nAuthor please");
                    String author = input1.nextLine();
                    System.out.println("\nGenre please");
                    String genre = input1.nextLine();
                    System.out.println("\nYear please");
                    int year = input1.nextInt();
                    
                    //add the book details to array
                    Book2 b = new Book2(title, author, genre, year);
                    bookar.add(bookcounter, B)/>;
                    bookcounter++;
        
                 break;
        
                 case 2: //list all books
                 for(int c = 0; c<bookcounter; c++){
                     System.out.println("\n==============================\n" + bookar.get(c));
                    }
                    System.out.println("\n==============================\n");
                 break;
                 
                 
                 case 3: //search for book by Title
                 
                 int searchCount;
                 String[] findTitle = new String[1];

                 Scanner titleSearch = new Scanner(System.in);
                 boolean found = false;
                 System.out.println("Please enter the Title of the book you wish to search");
                 findTitle[0] = titleSearch.next();
                 
                 //search array
                 for(int scount = 0; scount< bookar.size(); scount++){
                     
                     if(findTitle[0] == bookar[scount].getTitle()){
                         System.out.println("\n\n" + bookar[scount].getTitle() + "\n\n");
                         found = true;
                        }//if
                    }//for
                    
                    //error message
                    if(!found){
                        System.out.println("\n\nNothing Found\n\n");
                        System.out.println("\n\n" + bookar[0].getTitle() + "\n\n");
                    }//if
                 break;
                 
                 
                 case 4: //search for book by Author
                 break;
        
                 case 5: //search for book by Genre
                 break;
        
                 case 6: //search for book by Year
                 break;
        
                 case 7:
                    flag = false;
                 break;
        
                 default:
                 System.out.println("\n===============\nIncorrect input\n" +
                                    "===============\n");
                 break;
            }//switch
        }//while
    }//main
}//class


And of course the Book2.java file (which is the same as before)

public class Book2{
    private String title;
    private String author;
    private String genre;
    private int year;
    
    //constructor
    public Book2(String title, String author, String genre, int year){
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.year = year;
    }
    
    public String getTitle(){return title;}
    public String getAuthor(){return author;}
    public String getGenre(){return genre;}
    public int getYear(){return year;}
    
    public String toString(){
        return "\nTitle: " + title + "\nAuthor: " + author + "\nGenre: " + genre +"\nRelease Year: " + year + "\n";
    }
}


HELP PLEASE! I have spent hours going through my notes and text book and the internet to try to solve this without success! It's doing my head in (and I'm sure the answer will probably be simple...I just can't find it)

Thanks in advance. kosmro
Was This Post Helpful? 0
  • +
  • -

#7 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Searching and display with getMethods not working

Posted 28 September 2012 - 11:31 PM

Bookar is a ArrayList, yet you are treating it like a array by getting it with bookar[int].
Was This Post Helpful? 0
  • +
  • -

#8 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 28 September 2012 - 11:36 PM

I don't understand, how exactly are they different? I understand that the info is stored as an object under bookar, but I thought it was essentially the same for searching an ArrayList as is with and Array.

Sorry if I appear to be a little dumb on this, I don't quite understand it so it becomes tricky to work out for me untill I do understand it :/

This post has been edited by kosmro: 28 September 2012 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 29 September 2012 - 01:24 AM

Okay, I have had another shot at it, and I get the feeling that it is now just a sytax problem, but I'm now some what brain dead -_-;;

Here is just case3
boolean found = false;
                 Scanner titleSearch = new Scanner(System.in);
                 
                 Object search = titleSearch.next();
                 for(int scount = 0; scount < bookar.size(); scount++){
                     Object searched = bookar.get(scount);
                     boolean c = searched.contains(search);
                     if(c == true){
                         System.out.println("\n\n" + searched + "\n\n");
                         found = true;
                        }
                    }

                    if(!found){
                        System.out.println("\n\nNothing Found\n\n");
                    }//if
                    break;


The error that it is giving is as follows "can't find symbol - method contains(java.lang.Object)", and the ".contains" on line 7 is highlighted.
According to my book and stuff I have managed to finally find online, this SHOULD work.

This post has been edited by kosmro: 29 September 2012 - 01:25 AM

Was This Post Helpful? 0
  • +
  • -

#10 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Searching and display with getMethods not working

Posted 29 September 2012 - 03:22 AM

Well, I finally managed to get it to work!

For those looking in the future, here is the code in which I have used for case3;

case 3: //search for book by Title
                    Scanner sertitl = new Scanner( System.in );
                    System.out.println("\nPlease enter the Book Title: \n");
                    String s = sertitl.next();
                    for (Book2 x: bookar ){
                        if(x.getTitle().equals(s))
                        System.out.println(x.toString());
                        else
                        System.out.println("Book does not exist");
                    }
                 break;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1