954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Quick help with printing arrays

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..

nabil1983
Junior Poster in Training
73 posts since Mar 2005
Reputation Points: 13
Solved Threads: 0
 

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

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

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[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

nabil1983
Junior Poster in Training
73 posts since Mar 2005
Reputation Points: 13
Solved Threads: 0
 

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

nabil1983
Junior Poster in Training
73 posts since Mar 2005
Reputation Points: 13
Solved Threads: 0
 

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....

lolp1
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

emilchacko
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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.

emilchacko
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You