i m trying to do a music cd databse. i done most of it bt i dont kno how to do the search methord..can any 1 help

this is my program

import javax.swing.*;
import java.util.ArrayList;
import java.io.*;
import java.io.IOException;


public class Cd
{
public String ArtistName;
public String CdName;
public String Genre;
public int ReleasedDate;
public int NoOfTracks;


public Cd ()
{
ArtistName = JOptionPane.showInputDialog ("Enter The Artist's Name : ");
CdName = JOptionPane.showInputDialog("Enter The Cd Name :");
Genre = JOptionPane.showInputDialog("Enter The Music Genre :");
ReleasedDate = Integer.parseInt(JOptionPane.showInputDialog("Enter The ReleasedDate :"));
NoOfTracks = Integer.parseInt(JOptionPane.showInputDialog("Enter The Number Of Tracks :"));
}


public Cd (String a, String c, String g, int y, int n)
{
ArtistName = a;
CdName = c;
Genre = g;
ReleasedDate = y;
NoOfTracks = n;
}


public void printcd ()
{
String print = "Artist: " + ArtistName + " Cd: " + CdName + " Genre: " + Genre + " Year: " + ReleasedDate + " Tracks: " + NoOfTracks;


System.out.println(print);
}


public void printarray ()
{
String array = "Artist: " + ArtistName + " Cd: " + CdName + " Genre: " + Genre + " Year: " + ReleasedDate + " Tracks: " + NoOfTracks;



System.out.println(array);
}



}


class database
{
public static void main (String[] args)
{
int entry = 1;
Cd[] record = new Cd[entry];


final int SENTINEL = -1; // Sentinel block in a MultiShady chain.


String album;
String menu_choice;
int Menu;
int r;
int e;
int ARRAY_SIZE = 9;


Cd[] array = new Cd[ARRAY_SIZE];
{
array[0] = new Cd("Paul Weller",    "White Pony",   "Hard Rock",    2008,   21);
array[1] = new Cd("Bodyrox and Luciana",    "What Planet You On",   "Rock", 2008,   6);
array[2] = new Cd("Rem",    "Accelerate",   "Rock", 2008,   11);
array[3] = new Cd("Mariah Carey",   "E=mc2: Digipack",  "Pop",  2008,   10);
array[4] = new Cd("Leona Lewis",    "Spirit",   "Pop",  2008,   14);
array[5] = new Cd("Shayne Ward",    "Breathless",   "Pop",  2007,   13);
array[6] = new Cd("Mighty Underdogs",   "Prelude: Ep",  "Hip-Hop",  2007,   6);
array[7] = new Cd("Doniki", "Radikal Expressions",  "Reggae",   2008,   14);
array[8] = new Cd(" Billy Fury",    "The Complete Collection",  "Rock & Roll",  2008,   29);


}


menu_choice = JOptionPane.showInputDialog("To enter a new entry TYPE   0,\n To print database TYPE  1,\n  To Search TYPE    2,\n To Exit TYPE   3\n");
Menu = Integer.parseInt(menu_choice);


while (Menu != SENTINEL)
{
if (Menu == 0 )
{
for ( r = 0; r<entry; r++)
{
record[r] = new Cd();
}
for ( r = 0; r<entry; r++)
{
record[r].printcd();
}


menu_choice = JOptionPane.showInputDialog("To enter a new entry TYPE   0,\n To print database TYPE  1,\n  To Search TYPE    2,\n To Exit TYPE   3\n");
Menu = Integer.parseInt(menu_choice);


}


else if (Menu == 1)
{
for (e = 0; e<ARRAY_SIZE; e++)
{
array[e].printarray();
}


menu_choice = JOptionPane.showInputDialog("To enter a new entry TYPE   0,\n To print database TYPE  1,\n  To Search TYPE    2,\n To Exit TYPE   3\n");
Menu = Integer.parseInt(menu_choice);


}


else if (Menu == 3)
{
System.exit(0);
}


else if (Menu == 2)
{
album = JOptionPane.showInputDialog("Enter The Album To Search For :");


for ( e = 0; e<ARRAY_SIZE; e++)
{
if (array[e].CdName.equals(album))
array[e].printarray();
}
menu_choice = JOptionPane.showInputDialog("To enter a new entry TYPE   0,\n To print database TYPE  1,\n  To Search TYPE    2,\n To Exit TYPE   3\n");
Menu = Integer.parseInt(menu_choice);


}
}
}


public void fileoutput (Cd[] array) throws IOException
{
final FileWriter outputFile = new FileWriter("CD_Database.txt");
final BufferedWriter OutputBuffer = new BufferedWriter(outputFile);
final PrintWriter printstream = new PrintWriter(OutputBuffer);


for(int e = 0; e < array.length; e++)
{
printstream.println(array[e]);
printstream.close();
}


final FileReader inputFile = new FileReader("CD_Database.txt");
final BufferedReader inputBuffer = new BufferedReader(inputFile);


String line = inputBuffer.readLine();


}
}

Recommended Answers

All 4 Replies

1. what do you not exactly understand.
2. I recommend using a more flexible collection other than an array, maybe a vector, to store your cd records. That way when ever you can do:

//add records to collection
Vector<Cd> db= new Vector();
db.add(new Cd("Paul Weller", "White Pony", "Hard Rock", 2008, 21));
db.add(new Cd("Bodyrox and Luciana", "What Planet You On", "Rock", 2008, 6));
db.add(new Cd("Rem", "Accelerate", "Rock", 2008, 11));
db.add(new Cd("Leona Lewis", "Spirit", "Pop", 2008, 14));

//print db
for(int i= 0; i< db.size();i++)
    db.get(i).printarray();

Try this code and see how it works for you

import javax.swing.*;
import java.util.ArrayList;
import java.util.Vector;
import java.io.*;
import java.io.IOException;

// new class. all the details of the CD are declared and initialised.
class Cd
{
   public String ArtistName; 
   public String CdName; 
   public String Genre; 
   public int ReleasedDate; 
   public int NoOfTracks;

   // Ask the user to input the various details that makeup the Music CD
   public Cd ()
   {
      ArtistName = JOptionPane.showInputDialog ("Enter The Artist's Name : ");
      CdName = JOptionPane.showInputDialog("Enter The Cd Name :");
      Genre = JOptionPane.showInputDialog("Enter The Music Genre :");
      ReleasedDate = Integer.parseInt(JOptionPane.showInputDialog("Enter The ReleasedDate :"));
      NoOfTracks = Integer.parseInt(JOptionPane.showInputDialog("Enter The Number Of Tracks :"));
   }

   public Cd (String a, String c, String g, int y, int n)
   {

      ArtistName = a; 
      CdName = c; 
      Genre = g; 
      ReleasedDate = y;
      NoOfTracks = n;
   }

   public void printcd ()
   {
      String print = "Artist: " + ArtistName + " Cd: " + CdName + " Genre: " + Genre + " Year: " + ReleasedDate + " Tracks: " + NoOfTracks;

      System.out.println(print);
   }
 
   public void printarray ()
   {
      String array = "Artist: " + ArtistName + " Cd: " + CdName + " Genre: " + Genre + " Year: " + ReleasedDate + " Tracks: " + NoOfTracks;

      System.out.println(array); 
   }
}

public class database
{
// main method. array created as well.
public static void main (String[] args)
{ 
    final int entry = 1;
    Cd[] record = new Cd[entry]; 
    
    final int SENTINEL = -1; // Sentinel block in a MultiShady chain.
    
    String artist;
    String menu_choice;
    int Menu= 0;
    int r;
    int result;
    final int ARRAY_SIZE = 9; // initialising the array size,
    
    //add records to collection
    Vector<Cd> db= new Vector<Cd>();
    db.add(new Cd("Paul Weller", "White Pony", "Hard Rock", 2008, 21));
    db.add(new Cd("Bodyrox and Luciana", "What Planet You On", "Rock", 2008, 6));
    db.add(new Cd("Rem", "Accelerate", "Rock", 2008, 11));
    db.add(new Cd("Mariah Carey", "E=mc2: Digipack", "Pop", 2008, 10));
    db.add(new Cd("Leona Lewis", "Spirit", "Pop", 2008, 14));
    db.add(new Cd("Shayne Ward", "Breathless", "Pop", 2007, 13));
    db.add(new Cd("Mighty Underdogs", "Prelude: Ep", "Hip-Hop", 2007, 6));
    db.add(new Cd("Doniki", "Radikal Expressions", "Reggae", 2008, 14));
    db.add(new Cd("Billy Fury", "The Complete Collection", "Rock & Roll", 2008, 29));
    
    try{
        while (Menu != SENTINEL)
        {
            //the line of code below creates the menu options that will be used to run the program
            menu_choice = JOptionPane.showInputDialog("To enter a new entry TYPE 0,\n To print database TYPE 1,\n To Search TYPE 2,\n To Exit TYPE 3\n");
            Menu = Integer.parseInt(menu_choice);
            
            if (Menu == 0 ) // to add a new entry to the CD
            {
                for ( r = 0; r<entry; r++) 
                {
                    record[r] = new Cd();
                    record[r].printcd(); //was in separate loop before no need for that, condensed to one
                    
                    db.add(record[r]); //add new entry to collection
                }
    
            }
        
            // the else if below prints all the details that are currently stored in the music CD array.
            else if (Menu == 1)
            {
                for(int i= 0; i< db.size();i++)
                    db.get(i).printarray();
            
            }
            
            //search methord
            else if (Menu == 2)
            {
                artist = JOptionPane.showInputDialog("Enter The Artist To Search For :");
                
                result = linearSearch(db, artist, db.size());
                
                
                db.get(result).printarray();
                JOptionPane.showMessageDialog (null, "Artist: " + artist + " record found" ); 
            }
            // allows quit the program. 
            else if(Menu == 3)
            {
                System.exit(0);
            }
        }
    }catch(Exception e){
        System.exit(0);
    }
}//end main
public static int linearSearch(Vector<Cd> data, String key, int sizeOfArray)
{
    for (int counter = 0; counter < sizeOfArray; counter++)
    {
        if (data.get(counter).ArtistName.equals (key))
            return counter; // return position where found
        } 
        
        return -1; // if drop off end of array, its not there
    }
}
want exactly same problem but without using arraylist,linked list,GUI

problem should 

Load file
Create array of CDs

Sort CDs        ( artist,title,label,year,length)
Writing the sorted array to a file

Rozina: don't revive old threads, just start a new one.
exactly the same but without ...
that means it is not exactly the same. since you are using two separate datastructures, it isn't even nearly the same.

DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

This thread is closed.

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.