|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with java arrays!!
Hi all,
first timer novice doing a java program project and am stuck in places hope someone can help me out here.. Does neone know the code instruction to store an array of CD objects. Each entry in the array has to be a single object describing a CD. i.e: Artist Name , Album Name, Number of tracks i've written the program out but i cannot seem to get the program store each entry on its own and then display all the entries i have inputted,, instead it just displays the last entry i inputted.... import javax.swing.*; public class CdStorage { public static void main (String[] args) { String menu_choice; int menu; menu_choice = JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit"); menu = Integer.parseInt(menu_choice); CdRecord[] array = new CdRecord[5]; array[0] = new CdRecord(); while (menu == 1) { array[0] . artist_name = JOptionPane.showInputDialog("Enter Artist Name"); array[0] . album_name = JOptionPane.showInputDialog("Enter Album Name"); array[0] . no_of_tracks = Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks")); menu_choice = JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit"); menu = Integer.parseInt(menu_choice); // for (int i = 0; i < array.length; i++); if (menu == 2) array[0].printCdRecord(); //for (int i = 0; i < array.length; i++) { //System.out.println(array[i]);} if (menu == 3) System.out.println(); }//end while }//end main } // end class CdStorage 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; //stores first argument passed into artist_name album_name = album; //stores second argument passed into album_name no_of_tracks = tracks; //stores third argument passed into no_of_tracks } public CdRecord() { artist_name = "A"; album_name = "B"; no_of_tracks = 0; } public void printCdRecord (array[]) { int i = 0; for(i=0;i<array.length;i++) { System.out.println(array[i]); } System.out.println(); } }//end class cdstorage |
|
#2
|
||||
|
||||
|
First of all, I must admit I'm just a java amateur writing java web applications.
So, Swing is a big unknown to me. The fact that you're getting only the last CDRecord, doesn't surprise me. Within your while loop, you're only working with the O-th element of the array. You should keep track of the first free element of the array, using an integer and changing its value as soon as you have another CDRecord. Code:
array[0] = new CdRecord();
while (menu == 1)
{
array[0] . artist_name =
JOptionPane.showInputDialog("Enter Artist Name");
array[0] . album_name =
JOptionPane.showInputDialog("Enter Album Name");
array[0] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));
menu_choice =
JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit");
menu = Integer.parseInt(menu_choice);
|
|
#3
|
|||
|
|||
|
still cant get it
i've tried using the for loop to make the program move to the next array each time a entry
is made, but for some reason i cant seem to get the code right for this instruction.. Do you know the code for it??? |
|
#4
|
||||
|
||||
|
You cannot use the for loop because you don't know when the loop will stop,
or better said when the user will stop. Therefor, stick with the while loop, but still use the integer i you were using with the for loop. Write something like : Code:
CdRecord[] array = new CdRecord[5];
int i = 0;
while (menu == 1 && i < 5)
{
array[i] = new CdRecord();
array[i] . artist_name =
JOptionPane.showInputDialog("Enter Artist Name");
array[i] . album_name =
JOptionPane.showInputDialog("Enter Album Name");
array[i] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));
i++;
(...)
In short, you should add the integer i you already used in the for loop. This way , you keep track of the number of the element of the array you are using. In the while loop you should check if the value of i is less than 5 : the reason of this is that you've got only 5 elements in your array. And arrays start from 0 : so element 0 till 4 is valid. Element 5 would mean the sixth element in your array and you don't have space for that sixth element. As soon as you have put the values of the user in your CDRecord, you add 1 to the value of i : this means you're going to the next element in your array. |
|
#5
|
|||
|
|||
|
Still getting error
Ok thanx for the info,, i understand wat u say.. but the
problem is after making the correct changes, i can enter details but it still only prints out the last entry i input..... when i pass the array into the print method like: public void printCdRecord (array[]) i keeps saying identifier expected.. ne ideas y?? Last edited by nabil1983 : April 10th, 2005 at 10:14 PM. Reason: made changes to program |
|
#6
|
||||
|
||||
|
You should add a public method to the class CDRecord.
This should be something like Code:
public void printInfo ()
{
System.out.println(this.artist_name);
System.out.println(this.album_name);
...
}
You should call this method like this Code:
for(int i=0;i<array.length;i++)
{
CDRecord cdrec = (CDRecord) array[i]
cdrec.printInfo();
}
Sorry if it's not all correct (I suppose it isn't) but I wanted to answer you today. Won't have time tomorrow. Anyway : add a method to the class CDRecord and try to cast every element of the array to an object of the class CDRecord before you're displaying its info. |
|
#7
|
|||
|
|||
|
re:
Nope didnt work.
ne other suggestions neone please... |
|
#8
|
|||
|
|||
|
Quote:
because that declaration is malformed; do you mean... public void printCdRecord(CdRecord[] array) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Need help with java arrays!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|