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 13 of 13

Thread: .setText() for an int?

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default .setText() for an int?

    Hello everybody!

    Trying to make a calculator for my spanish teacher........... lol

    Here is teh source:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package src;
     
    /**
     *
     * @author Nick's Laptop
     */
    public class CalculatorFrame extends javax.swing.JFrame {
     
            int num1, num2;
            double output;
            char operator;
            boolean isNum1 = true;
            boolean isNum2 = false;
     
        /**
         * Creates new form CalculatorFrame
         */
        public CalculatorFrame() {
            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() {
     
            button1 = new javax.swing.JButton();
            button2 = new javax.swing.JButton();
            button3 = new javax.swing.JButton();
            button4 = new javax.swing.JButton();
            button5 = new javax.swing.JButton();
            button6 = new javax.swing.JButton();
            button7 = new javax.swing.JButton();
            button8 = new javax.swing.JButton();
            button9 = new javax.swing.JButton();
            buttonMinus = new javax.swing.JButton();
            button0 = new javax.swing.JButton();
            buttonPlus = new javax.swing.JButton();
            buttonEqual = new javax.swing.JButton();
            buttonMultiply = new javax.swing.JButton();
            buttonDivide = new javax.swing.JButton();
            textField = new javax.swing.JTextField();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Calculator");
     
            button1.setText("7");
     
            button2.setText("9");
     
            button3.setText("4");
     
            button4.setText("8");
     
            button5.setText("5");
     
            button6.setText("6");
     
            button7.setText("1");
            button7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button7ActionPerformed(evt);
                }
            });
     
            button8.setText("3");
     
            button9.setText("2");
            button9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button9ActionPerformed(evt);
                }
            });
     
            buttonMinus.setText("-");
     
            button0.setText("0");
     
            buttonPlus.setText("+");
            buttonPlus.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonPlusActionPerformed(evt);
                }
            });
     
            buttonEqual.setText("=");
            buttonEqual.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonEqualActionPerformed(evt);
                }
            });
     
            buttonMultiply.setText("*");
     
            buttonDivide.setText("/");
     
            textField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
            textField.setText("0");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonEqual, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(buttonMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonEqual, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>
     
        private void button7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
            if(isNum1 = true) {
     
                num1 = 1;
                textField.setText("1");
     
            }
     
            else {
     
                num2 = 1;
                textField.setText("1");
     
            }
     
        }                                       
     
        private void button9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
            if(isNum1 = true) {
     
                num1 = 2;
                textField.setText("2");
     
            }
     
            else {
     
                num2 = 2;
                textField.setText("2");
     
            }
     
        }                                       
     
        private void buttonEqualActionPerformed(java.awt.event.ActionEvent evt) {                                            
     
            textField.setText(output);
     
        }                                           
     
        private void buttonPlusActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
            operator = '+';
     
        }                                          
     
        /**
         * @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
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new CalculatorFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton button0;
        private javax.swing.JButton button1;
        private javax.swing.JButton button2;
        private javax.swing.JButton button3;
        private javax.swing.JButton button4;
        private javax.swing.JButton button5;
        private javax.swing.JButton button6;
        private javax.swing.JButton button7;
        private javax.swing.JButton button8;
        private javax.swing.JButton button9;
        private javax.swing.JButton buttonDivide;
        private javax.swing.JButton buttonEqual;
        private javax.swing.JButton buttonMinus;
        private javax.swing.JButton buttonMultiply;
        private javax.swing.JButton buttonPlus;
        private javax.swing.JTextField textField;
        // End of variables declaration
    }




    Note that I am not finished yet




    So, I think I have all the code figured out, except for this:

    private void buttonEqualActionPerformed(java.awt.event.ActionEv ent evt) {

    textField.setText(output);

    }

    The setText() gives me the error that I cant have an int as the text. I have tried using .toString(), but it says the int cannot be dereferenced.

    Help?

    -Silent


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: .setText() for an int?

    It cannot be dereferenced because int isn't an object. Integer however is.

    To solve your problem, you can use either of the following: setText(String.valueOf(23)) or setText( "" + 23)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    But the value will change, so I can't just have a certain number as the output. Is there a way to make this work?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: .setText() for an int?

    Use the variable you want to show instead of the 23
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    Thanks, that solved it!

  6. #6
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    Ok, new problem.

    I finished coding the number buttons. Here is what I have:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package src;
     
    /**
     *
     * @author Nick's Laptop
     */
    public class CalculatorFrame extends javax.swing.JFrame {
     
            int num1, num2;
            int output;
            char operator;
            int numCount = 1;
     
        /**
         * Creates new form CalculatorFrame
         */
        public CalculatorFrame() {
            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() {
     
            button1 = new javax.swing.JButton();
            button2 = new javax.swing.JButton();
            button3 = new javax.swing.JButton();
            button4 = new javax.swing.JButton();
            button5 = new javax.swing.JButton();
            button6 = new javax.swing.JButton();
            button7 = new javax.swing.JButton();
            button8 = new javax.swing.JButton();
            button9 = new javax.swing.JButton();
            buttonMinus = new javax.swing.JButton();
            button0 = new javax.swing.JButton();
            buttonPlus = new javax.swing.JButton();
            buttonEqual = new javax.swing.JButton();
            buttonMultiply = new javax.swing.JButton();
            buttonDivide = new javax.swing.JButton();
            textField = new javax.swing.JTextField();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Calculator");
     
            button1.setText("7");
            button1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button1ActionPerformed(evt);
                }
            });
     
            button2.setText("9");
            button2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button2ActionPerformed(evt);
                }
            });
     
            button3.setText("4");
            button3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button3ActionPerformed(evt);
                }
            });
     
            button4.setText("8");
            button4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button4ActionPerformed(evt);
                }
            });
     
            button5.setText("5");
            button5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button5ActionPerformed(evt);
                }
            });
     
            button6.setText("6");
            button6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button6ActionPerformed(evt);
                }
            });
     
            button7.setText("1");
            button7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button7ActionPerformed(evt);
                }
            });
     
            button8.setText("3");
            button8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button8ActionPerformed(evt);
                }
            });
     
            button9.setText("2");
            button9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button9ActionPerformed(evt);
                }
            });
     
            buttonMinus.setText("-");
            buttonMinus.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonMinusActionPerformed(evt);
                }
            });
     
            button0.setText("0");
            button0.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button0ActionPerformed(evt);
                }
            });
     
            buttonPlus.setText("+");
            buttonPlus.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonPlusActionPerformed(evt);
                }
            });
     
            buttonEqual.setText("=");
            buttonEqual.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonEqualActionPerformed(evt);
                }
            });
     
            buttonMultiply.setText("*");
            buttonMultiply.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonMultiplyActionPerformed(evt);
                }
            });
     
            buttonDivide.setText("/");
            buttonDivide.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonDivideActionPerformed(evt);
                }
            });
     
            textField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
            textField.setText("0");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonEqual, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(buttonMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buttonEqual, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>
     
        private void button7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
            if(numCount == 1) {
     
                num1 = 1;
                textField.setText("1");
                numCount++;
     
            }
     
            else {
     
                num2 = 1;
                textField.setText("1");
     
            }
     
        }                                       
     
        private void button9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
            if(numCount == 1) {
     
                num1 = 2;
                textField.setText("2");
     
            }
     
            else {
     
                num2 = 2;
                textField.setText("2");
     
            }
     
        }                                       
     
        private void buttonEqualActionPerformed(java.awt.event.ActionEvent evt) {                                            
     
            switch(operator) {
     
                case '+':
                    output = num1 + num2;
     
                case '-':
                    output = num1 - num2;
     
                case '*':
                    output = num1 * num2;
     
                case '/':
                    output = num1 / num2;
     
            }
     
            textField.setText(String.valueOf(output));
     
        }                                           
     
        private void buttonPlusActionPerformed(java.awt.event.ActionEvent evt) {                                           
     
            operator = '+';
     
        }                                          
     
        private void button8ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 3;
                textField.setText("3");
     
            }
     
            else {
     
                num2 = 3;
                textField.setText("3");
     
            }
     
        }
     
        private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 4;
                textField.setText("4");
     
            }
     
            else {
     
                num2 = 4;
                textField.setText("4");
     
            }        
     
        }
     
        private void button5ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 5;
                textField.setText("5");
     
            }
     
            else {
     
                num2 = 5;
                textField.setText("5");
     
            }      
     
        }
     
        private void button6ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 6;
                textField.setText("6");
     
            }
     
            else {
     
                num2 = 6;
                textField.setText("6");
     
            }        
     
        }
     
        private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 7;
                textField.setText("7");
     
            }
     
            else {
     
                num2 = 7;
                textField.setText("7");
     
            }        
     
        }
     
        private void button4ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 8;
                textField.setText("8");
     
            }
     
            else {
     
                num2 = 8;
                textField.setText("8");
     
            }        
     
        }
     
        private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 9;
                textField.setText("9");
     
            }
     
            else {
     
                num2 = 9;
                textField.setText("9");
     
            }        
     
        }
     
        private void button0ActionPerformed(java.awt.event.ActionEvent evt) {
     
            if(numCount == 1) {
     
                num1 = 0;
                textField.setText("0");
     
            }
     
            else {
     
                num2 = 0;
                textField.setText("0");
     
            }        
     
        }
     
        private void buttonMinusActionPerformed(java.awt.event.ActionEvent evt) {
     
            operator = '-';
     
        }
     
        private void buttonMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
     
            operator = '*';
     
        }
     
        private void buttonDivideActionPerformed(java.awt.event.ActionEvent evt) {
     
            operator = '/';
     
        }
     
        /**
         * @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
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new CalculatorFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton button0;
        private javax.swing.JButton button1;
        private javax.swing.JButton button2;
        private javax.swing.JButton button3;
        private javax.swing.JButton button4;
        private javax.swing.JButton button5;
        private javax.swing.JButton button6;
        private javax.swing.JButton button7;
        private javax.swing.JButton button8;
        private javax.swing.JButton button9;
        private javax.swing.JButton buttonDivide;
        private javax.swing.JButton buttonEqual;
        private javax.swing.JButton buttonMinus;
        private javax.swing.JButton buttonMultiply;
        private javax.swing.JButton buttonPlus;
        private javax.swing.JTextField textField;
        // End of variables declaration
    }

    When I run it, there aren't any errors, but when I try to do something really simple (like add 1 and 2) it always gives me 0 as the answer.

    I assume this is a really stupid error on my part, but I just can't find where it is! Can someone help?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: .setText() for an int?

    Add some printlns to show the values of the variables used to do the computation so you know if they are good values. Also print out the results.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    Well, I did that, and it still comes up with the same thing. However, when I use the divide button, it works fine. So I am assuming that I made an error somewhere along the actionListener codes for the operator buttons.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: .setText() for an int?

    Add a println after every computation to see if they are all working properly.

    You need to add this javac option to the javac commandline to get the warning telling you what is wrong: -Xlint
    Last edited by Norm; March 31st, 2012 at 08:39 PM. Reason: need -Xlint
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    SilentNite17 (April 1st, 2012)

  11. #10
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    WHat if Im using Netbeans/Eclipse?

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: .setText() for an int?

    The IDEs must have a way to display warnings about problems in the source code from the compiler that they use. I have no idea what they can do or how to control what they do.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: .setText() for an int?

    Nevermind, it worked with the command line. Thanks a lot Norm!!!

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: .setText() for an int?

    It seems like the IDE should give you the warnings.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. setText() NULL pointer excception problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2010, 08:32 PM