im doing a java project on creating a music database, i need to store an array of cd objects, each entry in the array will be a single object for a cd.
The objects are: Artist, Album, No of tracks

could sum1 point me in d right direction, thanks

im doing a java project on creating a music database, i need to store an array of cd objects, each entry in the array will be a single object for a cd.
The objects are: Artist, Album, No of tracks

could sum1 point me in d right direction, thanks

There are no "sum1"s here, leave the "text-speak" at the door and ask intelligent questions if you want intelligent answers.

That said, post the code that you have so far for your CD inventory homework and specific questions about the areas that you are stuck on.

I am creating a database program that stores details of CDs, I need to store an array of CD objects. I need help on creating an array. Each entry in the array will be a single object for a Cd.

import javax.swing.JOptionPane;


class Cd1
{
public static void main (String[] args)
{
String Artist_Name;
String Album_Name;
String str;
int Track_Number;



Artist_Name = JOptionPane.showInputDialog("Enter Artist's Name");
Album_Name = JOptionPane.showInputDialog("Enter Album's Name");
str     = JOptionPane.showInputDialog("Enter the Track Number ");
Track_Number  = Integer.parseInt(str);


JOptionPane.showMessageDialog(null, "Artist: " + Artist_Name + "\nAlbum_Name: " + Album_Name + "\nTrack Number: " + Track_Number);


System.exit(0);



}


}

Edited 3 Years Ago by happygeek: fixed formatting

You first need to create a class for the CD that stores those properties and provides methods to get and set each property.

thank you for the advice, i have taken your advice and Implemented it in d program. As well as the arrays, I need to create a menu of options as well, which include:

* new entry from keyboard
*print entries so far
*quit



import javax.swing.JOptionPane;


class Cd1
{
public static void main (String[] args)
{
int menu_choice;


CdRecord one = new CdRecord();


one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));



one.printCdRecord();
}



}


class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;


public CdRecord (String artist, String album, int tracks, int year)


{


artist_name = artist;
album_name = album;
no_of_tracks = tracks;


}
public CdRecord()


{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}


public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;


System.out.println(o);
}
}

Edited 3 Years Ago by happygeek: fixed formatting

Useful example for objects in arrays:

CdRecord [] records = null; : This is an array that takes CdRecord objects. THE ARRAY IS NULL

CdRecord [] records = new CdRecord[2]; : This array IS NOT NULL. The records IS an array and it behaves EXACTLY like any other array. But records[0] and records[1] ARE CdRecord objects and they are NULL.

CdRecord [] records = new CdRecord[2];
records[0]=new CdRecord();
records[1]=new CdRecord();

records[0],records[1] are CdRecord and they are not null. They behave exactly like any other CdRecord object:

records[0].artist_name="Artist";
System.out.println(records[1].artist_name);

Since you don't know how many CDs the user will enter then don't use an array. With arrays you NEED to know from the beginning their size. Use Vectors or ArrayLists.

I would suggest to alter the printCdRecord(). Every class inherits the toString() method from Object. It returns as a String all the information of the current instant of your class. So it would be better for you to do the same. Actually even if no one is forcing to do this (programmatically speaking) it is most preferable to have a toString() than a print method:
Your Code:

public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

System.out.println(o);
}

My suggestion:

public String toString()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

//System.out.println(o);
return o;
}

And call the above method in your main:
System.out.println(one.toString()); or better: System.out.println(one);

When you put an instant of a class in a System.out.println it will automatically call the toString() method. If you haven't implemented one it will the super class toString() and so on until it reaches the Object.toString().

Sorry for posting too many times but I think it is better to have different suggestions separated:

I need to create a menu of options as well, which include:
* new entry from keyboard
*print entries so far
*quit

A)Put your code inside a while(true) loop
B)Read from the keyboard the input:
String input=JOptionPane.showInputDialog("");
--If you give "read new entry" do whatever you are already doing but instead of printing it, put it inside a Vector"
--If you give "print" use a for-loop to print the entries
--If you give "quit" then use the command break; to exit the while(true) loop

Since you don't know how many CDs the user will enter then don't use an array. With arrays you NEED to know from the beginning their size. Use Vectors or ArrayLists.

I would suggest to alter the printCdRecord(). Every class inherits the toString() method from Object. It returns as a String all the information of the current instant of your class. So it would be better for you to do the same. Actually even if no one is forcing to do this (programmatically speaking) it is most preferable to have a toString() than a print method:
Your Code:

public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

System.out.println(o);
}

My suggestion:

public String toString()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

//System.out.println(o);
return o;
}

And call the above method in your main:
System.out.println(one.toString()); or better: System.out.println(one);

When you put an instant of a class in a System.out.println it will automatically call the toString() method. If you haven't implemented one it will the super class toString() and so on until it reaches the Object.toString().

I incorporated this into my program but when the user enters the artist, album, and track no, it doesn’t print, it prints
Artist Name: A
Album Name: B
No. Of Tracks: 0
Artist Name: A
Album Name: B
No. Of Tracks: 0

here's my code
import javax.swing.JOptionPane;

class Cd
{
public static void main (String[] args)
{
int menu_choice;

CdRecord one = new CdRecord();
System.out.println(one.toString());

one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));

}


}

class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;

public CdRecord (String artist, String album, int tracks, int year)

{

artist_name = artist;
album_name = album;
no_of_tracks = tracks;

}
public CdRecord()

{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}

public String toString()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

System.out.println(o);
return o;
}
}

You write:

public String toString()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

System.out.println(o);
return o;
}
.......
System.out.println(one.toString());

Meaning that you print inside the method, and then you print again. Remove the System.out.println() from the method:

public String toString()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

return o;
}

You also write:

CdRecord one = new CdRecord();
System.out.println(one.toString());

one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));

When you create the object:
CdRecord one = new CdRecord();
it has values: "A", "B", 0.
Then you call: System.out.println(one.toString()); So it prints the values that it currently has.
You give values to the object afterwards and you don't print them. So place the System.out.println() after you have given values to the object:

CdRecord one = new CdRecord();

one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));

System.out.println(one.toString());

thank you very much, that worked. Could you help me to store array of cd objects, there are 3 objects. how would i create an array using the class arraylist?

First of all how do you know that you will have exactly 3 objects. If this is the case then use arrays:
CDRecord [] records=new CDRecord[3]; And follow the example I sent you.
With arrays you cannot add more objects than the size of the array.

If you have a while-loop and the user can give as many CDRecord as he wants then use ArrayList: /java/util/ArrayList

ArrayList<CDRecord> records = new ArrayList<CDRecord>();

CDRecord cdr = new CDRecord();
//set the values of the cdr
records.add( cdr );

int length = records.size(); //returns how many objects the ArrayList has

CDRecord ret = records.get(i); //gets the CDRecord from the ArrayList that is at the 'i'th place. (i must be int)

the objects are:
artist name
album
number of tracks

These are not objects, they are variables of the object CDRecord:

class CDRecord {
String artistName;
String album;
int numberOfTracks;
}

The above is an object.
And I have seen your other post. I would suggest not to open a new post for the same thing. And most important do not have different versions of code in different posts.
Use the code you have at this post. Forget about the other. Add in this code the part where you create the menu with the if-statements but don't put anything inside them. Then start all over.
Put code in one of these options test it and then try to code the others. I saw that you tried to fix all the menu options and there are many different bugs. Try to build one by one.

Are you iash? Because I was referring to his code:

And I have seen your other post. I would suggest not to open a new post for the same thing. And most important do not have different versions of code in different posts.

Ive created the menu and added an array as well as a searchkey, but I getting 4 errors saying:

* cannot resolve symbol variable Record line 28
* cannot resolve symbol variable Record line 38
*cannot resolve symbol variable CdRecord line 47
*cannot resolve symbol variable CdRecord line 49

I can't seem to fix the problems. I'd appreciate if can get help with this.

My code:

import javax.swing.JOptionPane;


class NewCd
{
public static void main (String[] args)
{
int menu_choice;


CdRecord[] arrayrecord=new CdRecord[5];


int counter = 0;
int result;


CdRecord one = new CdRecord();


menu_choice=Integer.parseInt(JOptionPane.showInputDialog("Enter 1 to create cd"
+ "\nEnter 2 to print"
+ "\nEnter 3  to search"
+ "\nEnter 4 to quit" ));


while (menu_choice != 4)


{
if (menu_choice ==1 )
JOptionPane.showMessageDialog(null,"");
{


Record[counter] = new CdRecord();
counter++;
//for int (i)
}



if (menu_choice==2)
//JOptionPane.showMessageDialog(null,"");
{
for(int i=0;i<counter;i++)
Record.toString();
}


else if (menu_choice ==3)
//JOptionPane.showMessageDialog(null,"");
{


String searchKey =JOptionPane.showInputDialog("Please enter the model of the vehicle");


result=CdSearch(CdRecord,searchKey,counter);
if  (result >= 0) // a valid array index
CdRecord[result].CdRecord();


else
JOptionPane.showMessageDialog(null,"KEY " + searchKey + " NOT FOUND");
}


menu_choice=Integer.parseInt(JOptionPane.showInputDialog("Enter 1 to create cd"
+ "\nEnter 2 to print"
+ "\nEnter 3  to search"
+ "\nEnter 4 to quit" ));



}


//      System.out.println(one.toString());


}
public static int CdSearch (CdRecord [] data, String searchKey, int size)
{
for (int counter = 0; counter <size; counter++)
{
if (data[counter].toString().equals(searchKey))
return counter; // return position where found
}


return -1; // if drop off end of array, its not there
}



}

Edited 3 Years Ago by happygeek: fixed formatting

When you use code tags inside the [] write next to the code=JAVA so I can see the lines or write in the code where the error was, because I can't count your entire code to find the line that the error happened

You have declared an array:
CdRecord[] arrayrecord=new CdRecord[5];

and the method:

public static int CdSearch (CdRecord [] data, String searchKey, int size)
{
for (int counter = 0; counter <size; counter++)
{
if (data[counter].toString().equals(searchKey))
return counter; // return position where found
}

return -1; // if drop off end of array, its not there
}

takes an array as an argument. So you should call it with an array, not like this:
result=CdSearch(CdRecord,searchKey,counter);
CdRecord is not an array and you don't declare it anywhere. That is why you get the error.
Call trhe method like this:
result=CdSearch(arrayrecord,searchKey,counter);

And you shouldn't use the toString() for comparisons. It is only for returning the values of the object. In order for the above to work the searchKey should have exactly the structure that the toString returns. Based on what you want to compare them, meaning what attribute of the class you want to use in order to distinguish one instance from the other? If name is what you want then ask for the name from the dialog box and do this in the method:

data[counter].name.equals(searchKey) or if you have a getName:
data[counter].getName().equals(searchKey)

This article has been dead for over six months. Start a new discussion instead.