Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 28th, 2005, 8:04 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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));
  }
}
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Dec 28th, 2005, 9:01 AM   #2
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
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!

JD
JDStud6 is offline   Reply With Quote
Old Dec 28th, 2005, 1:21 PM   #3
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
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.
Master is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:17 AM.

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