Hello.
As i couldnt work out my problem with my other version, i re-wrote it in a totally different form,
now my program does display all the entries i make,, but the problem now is
how do stop the dialog box from keep coming up.

For example: i input 2 entries and thats all i want to input; i want those two entries to print out and thats it.

in this case i keep getting the input dialog box untill all my array elements are used up....
neone know how i can fix this problem???

All help apreciated.


import javax.swing.*;

public class CdStorage2
{
public static void main (String[] args)

{

CdRecord[] array = new CdRecord[5];

int i;


for(i=0; i<array.length; i++)

{
array = new CdRecord();

}


for (i=0; i<array.length;i++)
{
array.printCdRecord();
}

} //end main
}//end class

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


public CdRecord ()
{
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);

artist_name =
JOptionPane.showInputDialog("Enter Artist Name");

album_name =
JOptionPane.showInputDialog("Enter Album Name");

no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));

}
public void printCdRecord ()
{
System.out.println("Artist Name: " +artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " +no_of_tracks+"\n");

}
}//end class cdstorage

Try this out

import java.util.*;
import javax.swing.*;


public class CdStorage2
{
public static void main (String[] args)
{
CdRecord array;
List arrayLst= new ArrayList();


String menu_choice;
int menu;


while (true)
{
menu_choice =
JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit");
try {
menu = Integer.parseInt(menu_choice);
}
catch(NumberFormatException ne){
menu=1;
}
if (menu==3)
{
break;
}
else if (menu==1)
{
arrayLst.add(new CdRecord());
}
else if (menu==2)
{
Iterator iter = arrayLst.iterator();
while (iter.hasNext())
{
array = (CdRecord)iter.next();
array.printCdRecord();
}
break;
}
}


} //end main
}//end class


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



public CdRecord ()
{
artist_name =
JOptionPane.showInputDialog("Enter Artist Name");


album_name =
JOptionPane.showInputDialog("Enter Album Name");


no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));
}


public void printCdRecord ()
{
System.out.println("Artist Name: " +artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " +no_of_tracks+"\n");
}
}//end class cdstorage

Edited 3 Years Ago by Nick Evan: Fixed formatting

Ahhhh man it works excellent, thanx for tha help.

can u just tell me when i recomile it says:

Note: C:\Documents and Settings\Nabil\Desktop\CdStorage3.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

im using J Creator, wat does it mean by unchecked and unsafe operations.

also a couple of things that u did i havent learnt like:

catch(NumberFormatException ne){
menu=1;

Iterator iter = arrayLst.iterator();
while (iter.hasNext())

array = (CdRecord)iter.next();

ne chance of tellin what these pieces of code actually perform..

Thanks for the help though really really really apreciated :)

catch(NumberFormatException ne){
menu=1;
- This is just to default to menu 1 incase if user enters non numeric values

Iterator iter = arrayLst.iterator();
while (iter.hasNext())

array = (CdRecord)iter.next();

- Iterator is an object which is used to loop through.
- Using the Iterator get the objects which u added to the arraylist one by one.

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