Okay i've created an array object with help from here like so:
CdRecord[] array = new CdRecord[5];
array[0] = new CdRecord();

now i've used a while statement to write to the arrays , im writing CDName,AlbumName & NumberOfTracks...

when i run my program it all works except that when i want to print the entries made so far.. i just get the last one i input....
i want it to print out all the entries that i input.. neone know the code for that,, i've have used this to print out my arrays:

array[0].printCdRecord();


hope u can help..

Recommended Answers

All 8 Replies

Hi everyone,

I doubt there is a CDRecord class in java and thus am not sure why it is not working as i don't know what is in the printCDRecord method.

Basically you can do this to print the contents of arrays

public void print (array[])
{
int i = 0;

for(i=0;i<array.length;i++)
{
System.out.println(array[i]);
}

}

If that part of the array is not a string, don't forget to convert it to a string first. One possible reason why you are only getting the last value is that you could be forgetting to reset the counter value of the loop.

Richard West

Okay i've done wat u've told me, but i keep gettin errors. Below is the whole program, can u tell me where im going wrong with my array display...
if possible could u explain also as im kind of a novice at java..
Apreciate ne help..

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);}



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);
}


//  String o =
//  "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;


System.out.println();
}


}//end class cdstorage

could you explain to me how i reset the counter value for the loop please..?

Hi everyone,

I doubt there is a CDRecord class in java and thus am not sure why it is not working as i don't know what is in the printCDRecord method.

Basically you can do this to print the contents of arrays

public void print (array[])
{
int i = 0;

for(i=0;i<array.length;i++)
{
System.out.println(array[i]);
}

}

If that part of the array is not a string, don't forget to convert it to a string first. One possible reason why you are only getting the last value is that you could be forgetting to reset the counter value of the loop.

Richard West

Sorry this thread is old, I just googled a question about arrays and came up with this. Just wanted to add my question, I know how to print different array values with a counter, but is it possible to do the same thing with different arrays. I would like to make a program which prints ten lines each with a new array (and each array printing 10 values on that line, although I am ignoring this for the example.)

Is it possible to do something like....

for(i=0;i<10;i++)
{
System.out.println(line i[1]);
}

if i have ten arrays as line1, line2, line3.......


Thanks....

Do not hijack and bump old threads to add your question. Create a new thread for this. Read the forum rules.

int[] arr={20,30,40};
int[][] arr2d ={{20,30},{30,40}};
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.deeptoString(arr2d));

He said quick help. Any time in the first four years would've qualified.

He said quick help. Any time in the first four years would've qualified.

ya i know but wot to do i juz saw it 2day.i replied as soon as i cud..Any way it cud be used by any one who needs to know.it's not like only the one who asked the qn is going to read the reply's.

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.