Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   calculator (http://www.programmingforums.org/showthread.php?t=7741)

TecBrain Dec 28th, 2005 8:04 AM

calculator
 
I am currently working on a java calculator, I do have a little problem, if the user inputs the value of zero on the textbox on continues then only the first zero should be recognized.

For example:

000000 should be only recognized as 0, am not sure how to do that, also after the user click on the AC button the text area becomes 0 and if immediately enters a number then it becomes 04..... Any Help would be appreciated.


Thanks.


This is my code
:

/*
 * Main.java
 * Created on December 18, 2005, 12:09 PM
 *
 *
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class IntegerCalculator extends JApplet implements ActionListener{
//public class IntegerCalculator extends JApplet {

    /* graphical user interface components  */

        private JLabel label;
       
        private JTextField result;
        private final String names[] = {"7", "8", "9", "4","5","6", "1","2","3","0", "*", "/", "%", "+","-", "=", "AC"};
        private JButton button[];
        private JButton dumy1, dumy2, dumy3,dumy4;
        private boolean secondN = false;
        private boolean operator = false;
        private long n1 = 0, n2 =0 ;
        private String s1, s2;
        private int count = 0, oper = 18;
        private GridLayout grid1;
        private Container container;
  /*  public  void IntegerCalculator() {

    init();

    System.out.println("\n end init \n");
      }
    */

    /** set up GUI components*/

    public void init() {

// set up applet’s GUI

  // obtain content pane and set its layout to FlowLayout
        grid1 = new GridLayout(9, 3, 5, 5);
       
       
        container = getContentPane();
        container.setLayout( grid1 );

        //Container container = getContentPane();
        //container.setLayout( new GridLayout(9, 3, 2, 2) );

      // create numberLabel and attach it to content pane
        label = new JLabel("Basic Calculator", JLabel.CENTER);
       
        container.add(label);
        container.setBackground(Color.gray);
      // container.setForeground(Color.gray);

        result = new JTextField(9);
        //result.setBounds(10,10,50,10);
        result.setEditable(true);
        container.add(result);
        result.setText("");

      // register this applet as numberField’s ActionListener
      result.addActionListener( this );

      dumy1 = new JButton ("");
      dumy1.addActionListener(this);
      container.add(dumy1);
      dumy1.setVisible(false);
      dumy2 = new JButton ("");
      dumy2.addActionListener(this);
      container.add(dumy2);
      dumy2.setVisible(false);
      dumy3 = new JButton ("");
      dumy3.addActionListener(this);
      container.add(dumy3);
      dumy3.setVisible(false);
      dumy4 = new JButton ("");
      dumy4.addActionListener(this);
      container.add(dumy4);
      dumy4.setVisible(false);

      button = new JButton[names.length];
      for (int i=0; i < names.length; i++){
        button[i] = new JButton (names[i]);
        button[i].addActionListener(this);
        container.add(button[i]);
      }

    setSize( 300, 150 );
    setVisible(true);
    }

public void NumericB (int i){

  String s = new String();

    s = result.getText();
    s = s + String.valueOf(i);

    if (count < 10)
    {
        result.setText(s);
        count++;
    }

}

public void OperatorB (int i)
{
}


public void actionPerformed(ActionEvent e){

    Object src = e.getSource();
    String s = new String( "");

  //  s = result.getText();

    if (src  instanceof JButton)
    {

        if(src == button[0])
        {
        //        result.setText("");       
                NumericB(7);
        }

        else if(src == button[1])
        {
        //        result.setText("");
                NumericB(8);
        }
        else if(src == button[2])
        {
        //        result.setText("");
                NumericB(9);
        }
        else if(src ==  button[3])
        {
        //        result.setText("");
                NumericB(4);
        }
        else if(src == button[4])
        {
        //        result.setText("");
                NumericB(5);
        }
        else if(src ==  button[5])
        {
        //        result.setText("");
                NumericB(6);
        }
        else if(src == button[6])
        {
        //        result.setText("");
                NumericB(1);
        }
        else if(src ==  button[7])
        {
        //        result.setText("");
                NumericB(2);
        }
        else if(src ==  button[8])
        {
        //        result.setText("");
                NumericB(3);
        }
        else if(src ==  button[9])
        {
        //        result.setText("");
                NumericB(0);
        }
        else if(src ==  button[10]) {  // "*"
        {
                    oper = 11;
                    s = result.getText();
                    n1 = Long.parseLong(s);
                    count = 0;
                    operator = true;
                    result.setText("");
                }
        }
        else if(src ==  button[11] ) {    // "/"
                    oper = 12;
                    n1 = Long.parseLong(result.getText());
                    count = 0;
                    operator = true;
                    result.setText("");
        }
        else if(src ==  button[12] ) {    // "%"
                    oper = 13;
                    n1 = Long.parseLong(result.getText());
                    count = 0;
                    operator = true;
                    result.setText("");
        }
        else if(src ==  button[13] ) {    // "+"
                    oper = 14;
                    n1 = Long.parseLong(result.getText());
                    count = 0;
                    operator = true;
                    result.setText("");
        }
        else if(src ==  button[14] ) {    // "-"
                    n1 = Long.parseLong(result.getText());
                    count = 0;
                    operator = true;
                    result.setText("");
                    oper = 15;
        }

                else if(src == button[15]) {    // "="

                    if((operator == true)&&(oper!=0))
                    {
                        s = result.getText();
                        n2 = Long.parseLong(s);
                        makeCalculations();
                        oper = 0;
                       
                    }
        }
        else if(src == button[16] ) {    // "AC"
                    oper = 17;
                    n1 = Long.parseLong(result.getText());
                    count = 0;
                    operator = true;
                    result.setText("0");
                //button[16].setNextFocusableComponent(result);
        }


       
     

    }
}

private void makeCalculations(){

    long ans=0;
       
       
            switch (oper)
            {
            case 11:
                ans = n1 * n2;
                break;
            case 12:
                ans = n1/n2;
       
                break;
            case 13:
                ans = n1%n2;
                break;
            case 14:
                ans = n1+n2;
                break;
            case 15:
                ans = n1 - n2;
                break;
            }

  result.setText(String.valueOf(ans));
  }
}


JDStud6 Dec 28th, 2005 9:01 AM

for the 000000 -> 0:

add a for loop inside NumericB that takes out preceeding 0's.


for the 04 -> 4:

the above should take care of that too! :D

JD

Master Dec 28th, 2005 1:21 PM

basically check if the string size is greater than one, and if the first char in the string = 0, if so then replace the string with 0.


All times are GMT -5. The time now is 2:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC