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

Thread: Help codiing calculator

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Help codiing calculator

    I am trying to code a calculator using java but every time the = button is pressed a number format exception crops up. Can anyone see what's wrong in the code please ? Thanks
    Attached Images Attached Images


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help codiing calculator

    Hello FM010, welcome to the Java Programming Forums

    It's something to do with your String s.charAt() I think.

    There is a String Index Out of Bounds exception.

    The charAt() method returns a character value for the character at the given position of the String. This exception is telling us that you are trying to access a position that does not exist.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help codiing calculator

    int a = s.charAt(i);
    ...
    if(s.chatAt(a)){
       ...
    }

    This is your problem as JAvaPF stated its an out of range exception. the reason for it is when you do this s.charAt(i); you are expecting a value such as 3 to be assigned to a. where as what you actually get is the ascii code for the number 3 which is infact 51. So then you try and access the character at s51 which more that likely doesn't exist.

    EDIT: JavaPF did mention about the character ascii value being returned, i just missed it. Wasted post, sorry

    Hope that helps.
    Regards,
    Chris
    Last edited by Freaky Chris; June 5th, 2009 at 06:16 AM.

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help codiing calculator

    Thanks a lot your post was very useful. I am still having some trouble because a string index ou of bounds exception is cropping up.

    Thanks
    Attached Images Attached Images

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help codiing calculator

    Can you please post all of your code. I will take a look for you...

    Make sure you paste the code within code tags. Check my signature if you are unsure.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help codiing calculator

    This is the code. Thanks for your help.

     public class Calculator extends javax.swing.JFrame {
     
        /** Creates new form Calculator */
        public Calculator() {
            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.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            calculatorPanel = new javax.swing.JPanel();
            num1Button = new javax.swing.JButton();
            num4Button = new javax.swing.JButton();
            num3Button = new javax.swing.JButton();
            num2Button = new javax.swing.JButton();
            num5Button = new javax.swing.JButton();
            num6Button = new javax.swing.JButton();
            num7Button = new javax.swing.JButton();
            num8Button = new javax.swing.JButton();
            num9Button = new javax.swing.JButton();
            num0Button = new javax.swing.JButton();
            displayScreen = new javax.swing.JTextField();
            equalsButton = new javax.swing.JButton();
            plusButton = new javax.swing.JButton();
            minusButton = new javax.swing.JButton();
            multiplicationButton = new javax.swing.JButton();
            divisionButton = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            calculatorPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
     
            num1Button.setText("1");
            num1Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num1ButtonMouseReleased(evt);
                }
            });
     
            num4Button.setText("4");
            num4Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num4ButtonMouseReleased(evt);
                }
            });
     
            num3Button.setText("3");
            num3Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num3ButtonMouseReleased(evt);
                }
            });
     
            num2Button.setText("2");
            num2Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num2ButtonMouseReleased(evt);
                }
            });
     
            num5Button.setText("5");
            num5Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num5ButtonMouseReleased(evt);
                }
            });
     
            num6Button.setText("6");
            num6Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num6ButtonMouseReleased(evt);
                }
            });
     
            num7Button.setText("7");
            num7Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num7ButtonMouseReleased(evt);
                }
            });
     
            num8Button.setText("8");
            num8Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num8ButtonMouseReleased(evt);
                }
            });
     
            num9Button.setText("9");
            num9Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num9ButtonMouseReleased(evt);
                }
            });
     
            num0Button.setText("0");
            num0Button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    num0ButtonMouseReleased(evt);
                }
            });
     
            displayScreen.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    displayScreenActionPerformed(evt);
                }
            });
     
            equalsButton.setText("=");
            equalsButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    equalsButtonMouseReleased(evt);
                }
            });
     
            plusButton.setText("+");
            plusButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    plusButtonMouseReleased(evt);
                }
            });
     
            minusButton.setText("-");
            minusButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    minusButtonMouseReleased(evt);
                }
            });
     
            multiplicationButton.setText("*");
            multiplicationButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    multiplicationButtonMouseReleased(evt);
                }
            });
     
            divisionButton.setText("/");
            divisionButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    divisionButtonMouseReleased(evt);
                }
            });
     
            javax.swing.GroupLayout calculatorPanelLayout = new javax.swing.GroupLayout(calculatorPanel);
            calculatorPanel.setLayout(calculatorPanelLayout);
            calculatorPanelLayout.setHorizontalGroup(
                calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(calculatorPanelLayout.createSequentialGroup()
                    .addGap(29, 29, 29)
                    .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(displayScreen, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(calculatorPanelLayout.createSequentialGroup()
                            .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(calculatorPanelLayout.createSequentialGroup()
                                    .addComponent(num1Button)
                                    .addGap(18, 18, 18)
                                    .addComponent(num2Button)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(num3Button))
                                .addGroup(calculatorPanelLayout.createSequentialGroup()
                                    .addComponent(num4Button)
                                    .addGap(18, 18, 18)
                                    .addComponent(num5Button)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(num6Button))
                                .addGroup(calculatorPanelLayout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(num7Button)
                                    .addGap(18, 18, 18)
                                    .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(num0Button)
                                        .addGroup(calculatorPanelLayout.createSequentialGroup()
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(num8Button)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(num9Button)))))
                            .addGap(50, 50, 50)
                            .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(equalsButton)
                                .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(divisionButton, 0, 0, Short.MAX_VALUE)
                                    .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(minusButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(plusButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(multiplicationButton, 0, 0, Short.MAX_VALUE))))))
                    .addContainerGap(77, Short.MAX_VALUE))
            );
            calculatorPanelLayout.setVerticalGroup(
                calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(calculatorPanelLayout.createSequentialGroup()
                    .addGap(32, 32, 32)
                    .addComponent(displayScreen, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(num1Button)
                        .addComponent(num2Button)
                        .addComponent(num3Button)
                        .addComponent(plusButton))
                    .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(calculatorPanelLayout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addComponent(minusButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(multiplicationButton)
                            .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(calculatorPanelLayout.createSequentialGroup()
                                    .addGap(40, 40, 40)
                                    .addComponent(num0Button))
                                .addGroup(calculatorPanelLayout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(divisionButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(equalsButton))))
                        .addGroup(calculatorPanelLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(num4Button)
                                .addComponent(num5Button)
                                .addComponent(num6Button))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(calculatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(num7Button)
                                .addComponent(num8Button)
                                .addComponent(num9Button))))
                    .addContainerGap(220, Short.MAX_VALUE))
            );
     
            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(46, 46, 46)
                    .addComponent(calculatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(calculatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
        private void displayScreenActionPerformed(java.awt.event.ActionEvent evt) {                                              
     
    }                                             
     
        private void num1ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
                  displayScreen.setText(displayScreen.getText()+ num1Button.getText());   
        }                                        
     
        private void num2ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num2Button.getText()); // TODO add your handling code here:
        }                                        
     
        private void equalsButtonMouseReleased(java.awt.event.MouseEvent evt) {                                           
           int i=0;
           String s= displayScreen.getText();
            String num1= " ";
            String num2= " ";
            String res= " ";
            char achar=' ';
     
            int stringLength= s.length();
            char valueChar=(char) stringLength;
            int value= (int) valueChar;
     
           while(i!=value){
     
     
     
     
     
           do
           {
           		int charValue= s.charAt(i);
                    achar= (char) charValue;
     
                num1=num1 + achar;
                i++;
     
     
          } while(( achar!='+')||(achar!='-')||(achar!='*')||(achar!='/'));
     
           int number1= Integer.parseInt(num1);
     
     
     
     
            int a=s.charAt(i-1);
            char operator= (char) a;
     
     
            while (i!=value)
            {
            	int charValue= s.charAt(i);
            	char charPosition= (char) charValue;
     
                num2=num2+achar;
                i++;
            }
     
            int number2=Integer.parseInt(num2);
     
     
            if (operator=='+')
            {
                int result= number1+number2;
                res= " "+result;
                displayScreen.setText(res);
            }
     
             if (s.charAt(a)=='-')
            {
                int result= number1-number2;
                res= " "+result;
                displayScreen.setText(res);
            }
     
             if (operator=='*')
            {
                int result= number1*number2;
                res= " "+result;
                displayScreen.setText(res);
            }
     
             if (operator=='/')
            {
                int result= number1/number2;
                res= " "+result;
                displayScreen.setText(res);
            }
     
     
            num1= res;
          } 
        }                                          
     
        private void plusButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ plusButton.getText()); // TODO add your handling code here:
        }                                        
     
        private void num3ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num3Button.getText());  // TODO add your handling code here:
        }                                        
     
        private void num4ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
          displayScreen.setText(displayScreen.getText()+ num4Button.getText());   // TODO add your handling code here:
        }                                        
     
        private void num5ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            displayScreen.setText(displayScreen.getText()+ num5Button.getText()); // TODO add your handling code here:
        }                                        
     
        private void num6ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num6Button.getText());  // TODO add your handling code here:
        }                                        
     
        private void num7ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num7Button.getText());  // TODO add your handling code here:
        }                                        
     
        private void num8ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num8Button.getText());  // TODO add your handling code here:
        }                                        
     
        private void num9ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            displayScreen.setText(displayScreen.getText()+ num9Button.getText()); // TODO add your handling code here:
        }                                        
     
        private void num0ButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
           displayScreen.setText(displayScreen.getText()+ num0Button.getText());  // TODO add your handling code here:
        }                                        
     
        private void minusButtonMouseReleased(java.awt.event.MouseEvent evt) {                                          
            displayScreen.setText(displayScreen.getText()+ minusButton.getText()); // TODO add your handling code here:
        }                                         
     
        private void multiplicationButtonMouseReleased(java.awt.event.MouseEvent evt) {                                                   
            displayScreen.setText(displayScreen.getText()+ multiplicationButton.getText()); // TODO add your handling code here:
        }                                                  
     
        private void divisionButtonMouseReleased(java.awt.event.MouseEvent evt) {                                             
           displayScreen.setText(displayScreen.getText()+ divisionButton.getText());  // TODO add your handling code here:
        }                                            
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Calculator().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JPanel calculatorPanel;
        private javax.swing.JTextField displayScreen;
        private javax.swing.JButton divisionButton;
        private javax.swing.JButton equalsButton;
        private javax.swing.JButton minusButton;
        private javax.swing.JButton multiplicationButton;
        private javax.swing.JButton num0Button;
        private javax.swing.JButton num1Button;
        private javax.swing.JButton num2Button;
        private javax.swing.JButton num3Button;
        private javax.swing.JButton num4Button;
        private javax.swing.JButton num5Button;
        private javax.swing.JButton num6Button;
        private javax.swing.JButton num7Button;
        private javax.swing.JButton num8Button;
        private javax.swing.JButton num9Button;
        private javax.swing.JButton plusButton;
        // End of variables declaration                   
     
    }

  7. The Following User Says Thank You to FM010 For This Useful Post:

    chronoz13 (June 10th, 2009)

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help codiing calculator

    Your problem is with int = 0; in the equalsButtonMouseReleased(java.awt.event.MouseEven t evt) method.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #8
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help codiing calculator

    Is there any way how I could solve the problem please ?

    Thanks

Similar Threads

  1. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 12:29 PM
  2. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 16th, 2008, 12:42 AM