This discussion is archived
7 Replies Latest reply: Jun 1, 2009 3:18 PM by 807588 RSS

Binary Search Using String

807588 Newbie
Currently Being Moderated
I'm trying to perform a binary search on CDs stored in an arraylist but it will only work with the titles with no spaces (Such as Summertime & Heartless but not Dance Wiv Me). Is this a bug or will it simply not work with strings with a space in them? Also when it does work it will return the correct title but the artist and price aren't in the same arraylist index as the search value that was returned.
/**
 * Program to allow customers to purchase CDs from an online store
 * 
 * @author (Martin Hutton) 
 * @version (24/05/2009)
 */

import java.util.*;

public class CDs
{
    private Scanner input;
    private Scanner in;
    private Scanner sc;
    private CdList aCd;
    
    CDs()
    {
        this.aCd = new CdList();
        this.menu();
    }
    
    public void menu()
    {
        int select = 5;
        do
        {
            //Menu Display
            
            System.out.println("\n\t\t--== Main Menu ==---");
            System.out.println("\n\t\t1. View CDs");
            System.out.println("\n\t\t2. Purchase CDs");
            System.out.println("\n\t\t3. Search CDs");
            System.out.println("\n\t\t4. Sort CDs Titles");
            System.out.println("\n\t\t5. Exit");
            input = new Scanner(System.in);
            select = input.nextInt();
            switch (select)
            {
                case 1 : this.view(); 
                break;
                case 2 : this.purchase();
                break;
                case 3 : this.search();
                break;
                case 4 : this.sort();
                break;
                case 5 : exit();
                break;
                default : System.out.println("Error! Incorrect menu selection!");
            }
        }
        while ( select != 5 );
    }
    

    public void view()
    {
        System.out.printf("\f");//Clear screen
        System.out.println("\t\t--== Avaiable CDs ==--");
        System.out.println("");
        
        int size = aCd.getTitle().size();
        //loop to display array date
        for ( int i = 0; i < size; i++ )
        {
            
            System.out.println( "\t" + i + "\t" + aCd.getTitle().get(i)+ "\t\t\t" + aCd.getArtist().get(i) + "\t\t\t" + aCd.getPrice().get(i) + "\n");
        }
    }
    
    public void purchase()
    {
       System.out.printf("\f");//Clear screen
       double arrayPurchase[] = new double [15];
       in = new Scanner(System.in);
       sc = new Scanner(System.in);
       double total = 0.0;
       int itemindex = 0;
       
       System.out.println("How many CDs would you like to purchase? ");
       int amountNumbers = in.nextInt(); 
       for(int i = 0; i< amountNumbers; i++)
       {
           int q = itemindex;
          
           System.out.println("Please enter the CD number: ");
           q = in.nextInt();
           //aCd.getPrice().get(q);
           arrayPurchase[i] = aCd.getPrice().get(q);
           total += arrayPurchase;
}

System.out.println("\nThe total is: £" + total );

}

public void search()
{
System.out.println("\t\t--== Search CDs ==--\n");
System.out.println("Search for a CD: ");
String lc = input.next();
Collections.sort(aCd.getTitle());
int index = Collections.binarySearch(aCd.getTitle(),lc);

if ( index < 0 )
{
System.out.println("Sorry, CD not avaiable");
}

else
{
// System.out.println(aCd.getPrice().get(index));
System.out.println( index + aCd.getTitle().get(index) + "\t\t" + aCd.getArtist().get(index) + "\t\t" + aCd.getPrice().get(index));
}
this.aCd = new CdList();
}

public void sort()
{
System.out.println("\t\t-== Alphabetised CD Titles ==--\n");
Collections.sort(aCd.getTitle());
int size = aCd.getTitle().size();

for ( int i = 0; i < size; i++ )
{
System.out.println( "\t" + aCd.getTitle().get(i));
}

this.aCd = new CdList();
}

public void exit()
{
System.out.println("\nSystem shutting down...");
System.exit(0);
}
}


/**
* Write a description of class CdList here.
*
* @author (your name)
* @version (a version number or a date)
*/

import java.util.*;

public class CdList
{
//instance variables
private ArrayList<String> title;
private ArrayList<String> artist;
private ArrayList<Double> price;

public CdList()
{
//create instances
title = new ArrayList<String>();
artist = new ArrayList<String>();
price = new ArrayList<Double>();

//populate arrays
//add titles
title.add("Boom Boom Pow");
title.add("Summertime");
title.add("Number 1");
title.add("Shake It");
title.add("The Climb");
title.add("Not Fair");
title.add("Love Story");
title.add("Just Dance");
title.add("Poker Face");
title.add("Right Round");
title.add("Dance Wiv Me");
title.add("I'm Not Alone");
title.add("Hot 'n' Cold");
title.add("Viva La Vida");
title.add("Heartless");

//HEX codes
artist.add("Black Eyed Peas");
artist.add("Will Smith");
artist.add("Tinchy Stryder");
artist.add("Metro Station");
artist.add("Miley Cyrus");
artist.add("Lily Allen");
artist.add("Taylor Swift");
artist.add("Lady GaGa");
artist.add("Lady GaGa");
artist.add("Flo Rida");
artist.add("Dizzee Rascal");
artist.add("Calvin Harris");
artist.add("Katy Perry");
artist.add("ColdPlay");
artist.add("Kanye West");

//RGB Co-ods
price.add(0.99);
price.add(0.75);
price.add(1.99);
price.add(2.99);
price.add(2.99);
price.add(0.55);
price.add(2.75);
price.add(1.98);
price.add(1.25);
price.add(1.55);
price.add(0.99);
price.add(2.55);
price.add(0.55);
price.add(1.99);
price.add(0.99);
} //end of constructor

public ArrayList<String> getTitle()
{
return ( title );
} //end method

public ArrayList<String> getArtist()
{
return ( artist );
}//end method

public ArrayList<Double> getPrice()
{
return ( price );
}//end method

}