Hi i am trying to write a search function and the question i have is this, when i do something like this

        comix[0] = new Comic("book title", "Author", "isbn", 454.24F); 
         for (int i = 0; i < comix.length; i++) 
         { 
             Scanner console = new Scanner(System.in
                Scanner console = new Scanner(System.in);
                 String temp = console.nextLine();
                if ( temp.equals(comix[i].booktitle)){
                // print any thing that the user entered

and if the book title is something like "animals" and if i type in animal it does not show up. what i want to make is a search function that will allow me to search any amounts of arrays like the above with many parameters of book title, author etc and if the user enters something like "and" i want everything in all of my arrays that contain "and" to show up. im not sure if i should use arrays or a list or even vectors

plss help

Recommended Answers

All 8 Replies

Scanner console = new Scanner(System.in
Scanner console = new Scanner(System.in);

Declare this only once and before the loop or the first use of input
also post the complete code your working with

and if the book title is something like "animals" and if i type in animal it does not show up

the words are not equal since the first word has 1 more letter s at the end

and if the user enters something like "and" i want everything in all of my arrays that contain "and" to show up

check the substring method

im not sure if i should use arrays or a list or even vectors

arrays should be fine for this

import java.util.*;
public class ComicBooks { 

public ComicBooks() {}
public String manga() {
 // set up hash table
 Hashtable quality = new Hashtable();
 float price1 = 3.00F;
 quality.put("mint", price1);
 float price2 = 2.00F;
 quality.put("near mint", price2); 
 float price3 = 1.50F;
 quality.put("very fine", price3);
 float price4 = 1.00F;
 quality.put("fine", price4);
 float price5 = 0.50F;
 quality.put("good", price5);
 float price6 = 0.25F;
 quality.put("poor", price6);
 // set up collection




 Comic[] comix = new Comic[4];
 comix[0] = new Comic("Amazing Spider-Man", "1A", "very fine",9240.00F);
 comix[0].setPrice( (Float) quality.get(comix[0].condition));

 comix[1] = new Comic("the hulk", "181", "near mint",1325.00F);
 comix[1].setPrice( (Float) quality.get(comix[1].condition) );

 comix[2] = new Comic("the avengers", "1A", "good", 45.00F); 
 comix[2].setPrice( (Float) quality.get(comix[2].condition) );

 comix[3] = new Comic("avengers 10", "1A", "mint", 454.24F); 
comix[3].setPrice( (Float) quality.get(comix[3].condition) );



         for (int i = 0; i < comix.length; i++) 
         { 
             Scanner console = new Scanner(System.in);
             String temp = console.nextLine();


            if ( temp.substring(comix[i])){

             System.out.println("Title: " + comix[i].title);
             System.out.println("Issue: " + comix[i].issueNumber); 
             System.out.println("Condition: " + comix[i].condition);
             System.out.println("Price: $" + comix[i].price + "\n");
             }

                else if ( temp.equals(comix[i].title)){

                System.out.println("Title: " + comix[i].title);
                System.out.println("Issue: " + comix[i].issueNumber); 
                System.out.println("Condition: " + comix[i].condition);
                System.out.println("Price: $" + comix[i].price + "\n");}


                else if ( temp.equals(comix[i].issueNumber)){

                System.out.println("Title: " + comix[i].title);
                System.out.println("Issue: " + comix[i].issueNumber); 
                System.out.println("Condition: " + comix[i].condition);
                System.out.println("Price: $" + comix[i].price + "\n");}

                    else if ( temp.equals(comix[i].condition)){

                    System.out.println("Title: " + comix[i].title);
                    System.out.println("Issue: " + comix[i].issueNumber); 
                    System.out.println("Condition: " + comix[i].condition);
                    System.out.println("Price: $" + comix[i].price + "\n");}

             else
                 System.out.println("sorry bud no luck");
         }
        return null;



}
}
             class Comic {



          String title;

         String issueNumber; 
         String condition;
         float basePrice;
         float price;


         public String getTitle() 
            {return title;} 

            public String getIssue() 
            {return issueNumber;} 

            public String getCondition() 
            {return condition;}



         Comic(String inTitle, String inIssueNumber, String inCondition, float inBasePrice) 
         {
         title = inTitle;
         issueNumber = inIssueNumber; 
         condition = inCondition; 
         basePrice = inBasePrice;
         }
         void setPrice(float factor) 
         { 
             price = basePrice * factor;

         }


        }

so if i enter "avengers" i want those two arrays to show up, not just the one. but right now as it is, if i enter avengers as the title, it wont show up until the for loop, loops to the correct loop number

check the String api. My guess, the simplest way to do what you're trying to do is the contains method.

for instance: titles in your list:
"Avengers"
"avengers"
"The avengers"
"The Avengers"
...

here's your original code:

comix[0] = new Comic("book title", "Author", "isbn", 454.24F); 
         for (int i = 0; i < comix.length; i++) 
         { 
         //    Scanner console = new Scanner(System.in
         // I assume this line was a mistake??
                Scanner console = new Scanner(System.in);
                 String temp = console.nextLine();
                if ( temp.equals(comix[i].booktitle)){

as Zeroliken already said: you're causing a lot of overhead re-declaring and re-initializing your console. it's not supposed to change, so once 'll do just fine.
just take a look at this:

String[] titles = {"the Avengers","avengers","Iron Man","Thor", "Iron Man, Thor and the other avengers"};
Scanner console = new Scanner(System.in);
System.out.println("Enter a title: \n");
String temp = console.nextLine();
temp = temp.toLowerCase();
for ( String title: titles){
  if ( title.toLowerCase().contains(temp))
    System.out.println(title);
}

as pointed out earlier: "avenger" and "avengers" are not identical, so equals will not return true, when these two are compared. you may have an 'equalsIgnoreCase', but, there is no 'containsIgnoreCase'. if you want both "avengers" and "Avengers" to return the same result, you'll have to put both fields (the input and the one you compare it to) to either both all uppercases, or all lowercases (without changing the data in your list, of course), hence my two .toLowerCase() calls.

Thanks that did help, but what i was also wondering was if i were to put stuff into a mulitdimental array such as

String [] [] TESTER = {{Apple, apple, the tree, the orange, the banana }, {apple, the tv, the computer, the colour orange, macbook}}

now how would i be able to search for "the" and my result be (the tree, the orange, the banana) and (the tv, the computer, the colour orange) i want everything that contains "the" to show.

first of... why would you put it in a two dimensional array?
for the rest, just the same as my example, you'll just have to use a nested iteration.

the first iteration will get a 'normal' array of Strings as element:

    for ( String[] oneDimArray : twoDimArray){    
          for ( String title: oneDimArray ){
            if ( title.toLowerCase().contains(input.toLowerCase())
              System.out.println(title);
          }
        } 
for (int i = 0; i < comix.length; i++)
    {
    Scanner console = new Scanner(System.in);
    String temp = console.nextLine();

Your problems start here.
Your loop starts with i = 0. It gets the search word from the user, and tests the first element of comix.
It then goes to the second element, and forgets the original search word, and gets a new one from the user, which it compares with the second element of comix.
Ie because you get the user input inside the loop, each search word is only compared with one element of comix, not with them all.

first of... why would you put it in a two dimensional array?
for the rest, just the same as my example, you'll just have to use a nested iteration.the first iteration will get a 'normal' array of Strings as element

    String [] [] TESTER = {{"Apple", "apple", "the tree", "the orange", "the banana"},{"apple2", "the tv", "the computer", "the colour orange", "macbook"}};    
            for ( String[] oneDimArray : TESTER){  
              for ( String title: oneDimArray ){
                  if ( title.toLowerCase().contains(b))
                    System.out.println(title);      
             }
         }

how would i be able to put the two arrays together? like for example when i call this and let say i enter "orange"
i want it to display "the orange the colour orange" on the same line.

How about update your System.out.println(title); to System.out.print(title+" "); instead? Also, just insert System.out.println(); wherever you want to have a new line.

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.