Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help with my Netbeans calculator code

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with my Netbeans calculator code

    I am trying to create a retail calculator that will show sales prices of items. A user enters the original price of the item in a text box, and the percentage discounted in another box. The user then clicks the calculate button to get the answer. My problem is that I cannot get the program to calculate the answer, instead I am getting a lot of error messages. I cannot figure out how to solve these issues. Below is the codes I have:

    import javax.swing.JOptionPane;


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * @author Owner
    */
    public class RetailCalcForm extends javax.swing.JFrame {

    private void close() {
    throw new UnsupportedOperationException("Not yet implemented");
    }

    private static class number {

    public number() {
    }
    }

    private static class number2 {

    public number2() {
    }
    }

    /**
    * Creates new form RetailCalcForm
    */
    public RetailCalcForm() {
    initComponents();
    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    itemNameText = new javax.swing.JTextField();
    originalPriceText = new javax.swing.JTextField();
    percentDiscountedText = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    calculateButton = new javax.swing.JButton();
    salesPriceLabel = new javax.swing.JLabel();
    exitButton = new javax.swing.JButton();
    javax.swing.JComboBox departmentBox = new javax.swing.JComboBox();
    jLabel4 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel1.setText("Item Name");

    jLabel2.setText("Original Price");

    jLabel3.setText("% Discounted");

    calculateButton.setText("Calculate Discount Price");
    calculateButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    calculateButtonActionPerformed(evt);
    }
    });

    salesPriceLabel.setText("Discounted Sales Price: ");

    exitButton.setText("Exit");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    exitButtonActionPerformed(evt);
    }
    });

    departmentBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Electronics", "Toys", "Clothing", "Housewares", "Garden" }));
    departmentBox.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    departmentBoxActionPerformed(evt);
    }
    });

    jLabel4.setText("Select Department");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(28, 28, 2Cool
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jLabel2)
    .addComponent(jLabel1))
    .addContainerGap())
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(salesPriceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jLabel3)
    .addComponent(jLabel4))
    .addGap(26, 26, 26)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(percentDiscountedText, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(departmentBox, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(originalPriceText, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(itemNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 59, Short.MAX_VALUE)
    .addComponent(calculateButton)))))
    .addGap(0, 33, 33))))
    .addGroup(layout.createSequentialGroup()
    .addGap(228, 228, 228)
    .addComponent(exitButton)
    .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(69, 69, 69)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(departmentBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel4))
    .addGap(35, 35, 35)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(jLabel1)
    .addComponent(itemNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(37, 37, 37)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel2)
    .addComponent(originalPriceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(26, 26, 26)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(percentDiscountedText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3))
    .addGap(35, 35, 35)
    .addComponent(salesPriceLabel)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 44, Short.MAX_VALUE)
    .addComponent(exitButton)
    .addGap(26, 26, 26))
    .addGroup(layout.createSequentialGroup()
    .addGap(140, 140, 140)
    .addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void calculateButtonActionPerformed(java.awt.event.Acti onEvent evt) {
    int number1 = 0, number2 = 0;
    try (
    int = Integer.parseInt(
    this.originalPriceText.getText());
    catch (Excemption e) {
    JOptionPane.showMessageDialog(this, "Invalid Price Amount", "Error", JOptionPane.ERROR_MESSAGE);
    return;
    }
    try (
    int = Integer.parseInt(
    this.percentDiscountedText.getText());
    catch (Excemption e) {
    JOptionPane.showMessageDialog(this, "Invalid Discount Amount", "Error", JOptionPane.ERROR_MESSAGE);
    return;
    }
    int answer = number1 / number2;
    this.salesPriceLabel.setText(
    "Discounted Sales Price: " / answer);
    }

    private void departmentBoxActionPerformed(java.awt.event.Action Event evt) {
    // TODO add your handling code here:
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    System.exit(0);
    close();

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /*
    * Set the Nimbus look and feel
    */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
    * If Nimbus (introduced in Java SE 6) is not available, stay with the
    * default look and feel. For details see
    * How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(RetailCalcForm. class.getName()).log(java.util.logging.Level.SEVER E, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(RetailCalcForm. class.getName()).log(java.util.logging.Level.SEVER E, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(RetailCalcForm. class.getName()).log(java.util.logging.Level.SEVER E, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(RetailCalcForm. class.getName()).log(java.util.logging.Level.SEVER E, null, ex);
    }
    //</editor-fold>

    /*
    * Create and display the form
    */
    java.awt.EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
    new RetailCalcForm().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton calculateButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JTextField itemNameText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField originalPriceText;
    private javax.swing.JTextField percentDiscountedText;
    private javax.swing.JLabel salesPriceLabel;
    // End of variables declaration
    }


    Any help I can get will be greatly appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: I need help with my Netbeans calculator code

    When posting code, please use the highlight tags. Unfromatted code is pretty hard to read.

    What errors are you encountering? What lines do they occur on? The best way to get help is to put together an SSCCE that demonstrates the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Age Calculator
    By Sagittarian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2012, 08:06 PM
  2. Java Source Code to UML Diagrams in NetBeans
    By sarkuzi in forum Threads
    Replies: 3
    Last Post: February 7th, 2012, 10:34 AM
  3. having bad time with basic calculator code
    By hashey100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 08:29 PM
  4. Help needed with calculator code
    By andys in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2011, 08:02 PM
  5. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM