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

Dani AI

Generated

The behavior seen in the original post (dialogs repeating until the array fills) comes from creating a fixed-size array and instantiating a CdRecord for every slot while the constructor itself drives all the user I/O. fixed the visible symptom by moving the menu into a loop and storing entries in a resizable collection, which is the right direction — separate the data model (CdRecord) from the UI flow so construction doesn’t always trigger input dialogs.

Practical suggestions to harden the code:

  • Make CdRecord a plain data holder (fields, getters/setters). Put all JOptionPane calls in the menu/flow code or a dedicated factory method such as CdRecord.fromInput(). That makes it easy to stop asking for input.

  • Use a resizable, generic collection to avoid the compiler warning and to let the user add as many entries as needed:

    List<CdRecord> list = new ArrayList<CdRecord>();
  • Handle Cancel/close and bad numeric input explicitly. JOptionPane.showInputDialog(...) returns null if the user cancels; treat that as “quit” or “go back”. Parse integers inside a try/catch and re-prompt or default if parsing fails:

    String s = JOptionPane.showInputDialog("Tracks:");
    if (s == null) { /* user cancelled — break or return */ }
    try { tracks = Integer.parseInt(s); } 
    catch (NumberFormatException ex) { /* re-ask or set a default */ }

About the compiler message: “uses unchecked or unsafe operations” appears because raw collections are used (no generics). Recompile with javac -Xlint:unchecked FileName.java to see exact lines. Fix it by using generics (List<CdRecord>, Iterator<CdRecord>), or as a last resort annotate @SuppressWarnings("unchecked") on the small, vetted spots.

Design notes: avoid doing UI work inside constructors, prefer factory methods, and consider JOptionPane.showOptionDialog or showConfirmDialog for menus so numeric parsing isn’t required. Also decide whether Print should return to the menu or terminate — remove the break after printing if continuation is desired. These changes will stop the endless dialogs and make the program safer and clearer.

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

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.

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.