Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3942794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:42:20+00:00 2026-05-20T00:42:20+00:00

I have to create a CD inventory program for my first Java class. The

  • 0

I have to create a CD inventory program for my first Java class. The book is poorly written and extremely verbose. I have created 4 frames to handle each requirement of the assignment. But the book doesn’t explain how to write arrays to a .dat file. If I could get an idea of how to add data to an array from my TextFields then write to a .dat file I could stumble through the rest. Here is what I have so far. How do I take my JTextFields from my Add CD listener and write to a .dat file so I can view it later.

import java.util.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;



class CDinventoryItem extends JFrame implements Comparable <CDinventoryItem> {

    private String sPtitle;
    private String genreCD;
    private int iPitemNumber;
    private int iPnumberofUnits;
    private double dPunitPrice;
    private double dEvalue;
    private JFrame frame2= new JFrame();
    private JFrame frame3= new JFrame();
    private JFrame frame4= new JFrame();
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JPanel panel3 = new JPanel();
    private JPanel panel4 = new JPanel();
    private JPanel panel5 = new JPanel();
    private JPanel panel6 = new JPanel();
    private JLabel[] label = new JLabel[20];
    private JTextField titleField;
    private JTextField itemNField;
    private JTextField numofunitsField;
    private JTextField priceField;
    private JButton next;
    private JButton prev;
    private JButton addCD = new JButton ("Add CD");
    private JButton save = new JButton ("Save");
    private JButton delete;
    private JButton modify;
    private JButton search = new JButton ("Search for CD");
    private JButton mainmenu;
    private JButton displayCD = new JButton ("Display Inventory");
    private CDinventoryItem [] inven;
    private DataOutputStream outFile;
    private DataInputStream inputFile;



    public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
    double unitPrice, String genre){

            sPtitle = title;
            iPitemNumber = itemNumber;
            iPnumberofUnits = numberofUnits;
            dPunitPrice = unitPrice;
            genreCD = genre;


        for(int i = 0; i < label.length; i++) {
        label[i] = new JLabel();
        }

        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1.add(label[0]);
        Icon bug = new ImageIcon( getClass().getResource( "mylogo.JPG" ) );
        label[0].setIcon( bug );
        label[0].setPreferredSize(new Dimension(400, 150));

        panel1.add(label[1]);
        label[1].setText("Press button to choose option:");
        label[1].setPreferredSize(new Dimension(400, 50));


        panel1.add(panel2);
        panel2.setPreferredSize(new Dimension(400, 50));
        ButtonListerner inputNewCD = new ButtonListerner();
        panel2.add(addCD);
        addCD.addActionListener(inputNewCD);
        ButtonListerner searchCD = new ButtonListerner();
        panel2.add(search);
        search.addActionListener(searchCD);

        ButtonListerner display = new ButtonListerner();
        panel2.add(displayCD);
        displayCD.addActionListener(display);

        setContentPane(panel1);

    }


    private class ButtonListerner implements ActionListener
    {

        public void actionPerformed ( ActionEvent event) {


        if (event.getActionCommand().equals("Add CD"))
        {
            frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame2.setLocation(525,100);
            frame2.setSize(425, 425);
            frame2.setVisible( true );


            frame2.add(panel3);

            panel3.add(label[0]);

            panel3.add(label[2]);
            label[2].setText("Enter title of CD:");
            label[2].setPreferredSize(new Dimension(100, 25));

            JTextField titleText = new JTextField(20);
            panel3.add (titleText);
            sPtitle.equals(titleText);
            label[2].setPreferredSize(new Dimension(175, 25));

            panel3.add(label[3]);
            label[3].setText("Enter number of CDs:");

            JTextField numCDText = new JTextField(5);
            panel3.add(numCDText);
            label[3].setPreferredSize(new Dimension(275, 25));

            panel3.add(label[4]);
            label[4].setText("Enter price of CD:");
            JTextField priceCDText = new JTextField(6);
            panel3.add(priceCDText);

            label[4].setPreferredSize(new Dimension(274, 25));

            panel3.add(label[5]);
            label[5].setText("Pick genre:");
            String stringBox[] = {"Drama","Action","Comedy"};
            JComboBox comboBox = new JComboBox(stringBox);
            comboBox.setEditable(false);
            panel3.add(comboBox);

            panel3.add(save);

}

        if (event.getActionCommand().equals("Search for CD")){


            frame3.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame3.setLocation(100,525);
            frame3.setSize(425, 425);
            frame3.setVisible( true );

            frame3.add(panel4);

            panel4.add(label[0]);

            panel4.add(label[6]);
            label[6].setText("Enter name of CD you want to search for:");
            JTextField searchName = new JTextField(20);
            panel4.add(searchName);

            panel4.add(label[7]);
            label[7].setText("Or search by genre:");
            JCheckBox checkB1 = new JCheckBox("Drama");
            JCheckBox checkB2 = new JCheckBox("Action");
            JCheckBox checkB3 = new JCheckBox("Comedy");
            panel4.add(checkB1);
            panel4.add(checkB2);
            panel4.add(checkB3);


        }

        if (event.getActionCommand().equals("Display Inventory")){

            frame4.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame4.setLocation(525,525);
            frame4.setSize(425, 425);
            frame4.setVisible( true );

            frame4.add(panel5);

            panel5.add(label[0]);

            panel5.add(label[8]);
            label[8].setText("List of CDs:");

        }

        if (event.getActionCommand().equals("Save")){

            String dataFile = "inventory.dat";

            try{
            outFile = new DataOutputStream(new BufferedOutputStream(
            new FileOutputStream(dataFile)));
            }
            catch(FileNotFoundException fileNotFoundException ){

            }

        }
}}

    public int compareTo(CDinventoryItem otherItem) {
    return this.sPtitle.compareTo(otherItem.getTitle());
}

@Override
    public String getTitle() {
        return sPtitle;
}

}


public class CDinventoryprogram {


public static void main(String[] args) {

    JOptionPane.showMessageDialog( null, "Welcome to my CD inventory program!!"
,  "Inventory", JOptionPane.INFORMATION_MESSAGE);

    CDinventoryItem initem = new CDinventoryItem ("", 0, 0, 0.0,"" );
    initem.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    initem.setLocation(100,100);
    initem.setSize(425, 425);
    initem.setVisible( true );

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:42:20+00:00Added an answer on May 20, 2026 at 12:42 am

    In the method ButtonListerner.actionPerformed(), when the Add CD button is pressed (checked by event.getActionCommand().equals(“Add CD”)), you are creating a local text field:

    JTextField titleText = new JTextField(20);
    panel3.add (titleText);
    sPtitle.equals(titleText);
    label[2].setPreferredSize(new Dimension(175, 25));
    
    panel3.add(label[3]);
    label[3].setText("Enter number of CDs:");
    
    JTextField numCDText = new JTextField(5);
    panel3.add(numCDText);
    label[3].setPreferredSize(new Dimension(275, 25));
    ...
    

    The first thing you need to do is use the one in the outer class (so you can get the values later):

    titleField = new JTextField(20);
    panel3.add (titleField);
    sPtitle.equals(titleField);
    label[2].setPreferredSize(new Dimension(175, 25));
    
    panel3.add(label[3]);
    label[3].setText("Enter number of CDs:");
    
    itemNField = new JTextField(5);
    panel3.add(itemNField);
    label[3].setPreferredSize(new Dimension(275, 25));
    ...
    

    After this, when Save button is clicked, now your fields will hold the user input, so you can now get those values:

    String title = titleField.getText();
    ...
    

    Now to write to a file, you need to use an OutputStream. There are many OutputStream subclasses, each one has it’s own use. BufferedWriter is for writing text, DataOutputStream is for writing binary data, you should not use both together like you are doing now. Assuming you want to write binary data, you can do this:

    try {
        outFile = new DataOutputStream(new FileOutputStream(dataFile));
    
        outFile.writeUTF(titleField.getText());
    
        // convert string to int
        int itemN = Integer.parseInt(itemNField.getText());
        outFile.writeInt(itemN);
    
        outFile.flush();
        outFile.close();
    }
    catch(IOException error) {
        error.printStackTrace();
    }
    

    Note: To keep things simple, I didn’t added proper error handling (the outFile.close() should be in a finally statement).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create the sin function from scratch in my Comp Sci class,
This is what I currently have: CREATE OR REPLACE TRIGGER MYTRIGGER AFTER INSERT ON
I have to create a dialog based application, instead of old CFormView type of
I have to create a site definition for a client that must contain pre-defined
i have to create an asp.net page dynamically on runtime. It should work like
Is it possible to create/have a non-modal .net OpenFileDialog I have a UI element
I'm creating a custom WSS workflow in Visual Studio 2008 and have a 'create
Does ASP.NET MVC provide a standard validator functionality or do you have to create
I'm currently looking into unit testing for a new application I have to create.
This is kind of a weird problem, but I have to create a search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.