Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 9th, 2007, 09:01 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
Quick Help With Calculator

I cannot figure out why the *, /, and - sign do not work any help appricated thx!

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class caculatorButtons extends JFrame implements ActionListener
{
    private JTextField displayText = new JTextField(30);
    private JButton[] button = new JButton[20];
    private String[] keys = {"MC", "MR", "M-", "M+",
                             "7", "8", "9", "/",
                             "4", "5", "6", "*",
                             "1", "2", "3", "-",
                             "0", "C", "=", "+"};

    private String numStr1 = "";
    private String numStr2 = "";

    private char op;
    private boolean firstInput = true;

    public  caculatorButtons()
    {
        setTitle ("My Calculator");
        setSize (270, 300);
        Container pane = getContentPane();
        pane.setLayout(null);

        displayText.setSize (240, 30);
        displayText.setHorizontalAlignment(SwingConstants.  RIGHT);
        displayText.setLocation (10,10);
        pane.add(displayText);

        int x, y;

        x = 10;
        y = 40;
        for (int ind = 0; ind < 20; ind++)
        {
            button[ind] = new JButton (keys[ind]);
            button[ind].addActionListener(this);
            button[ind].setSize(60, 30);
            button[ind].setLocation (x, y);
            pane.add(button[ind]);
            x = x + 60;
            if ((ind + 1) % 4 == 0 )
            {
                x = 10;
                y = y + 30;
            }
        }

        this.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
        );

        setVisible (true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed (ActionEvent e)
    {
        String resultStr;

        String str = String.valueOf (e.getActionCommand());

        char ch = str.charAt(0);

        switch (ch)
        {
        case '0': case '1': case '2':
        case '3': case '4': case '5':
        case '6': case '7': case '8':
        case '9':

            if (firstInput)
            {
                numStr1 = numStr1 + ch;
                displayText.setText (numStr1);
            }
            else
            {
                numStr2 = numStr2 + ch;
                displayText.setText(numStr2);
            }
            break;

        case '+': case '-': case '*': case '/':
        op = ch;
        firstInput = false;
            break;
        case '=': resultStr = evaluate();
                  displayText.setText(resultStr);
                  numStr1 = resultStr;
                  numStr2 = "";
                  firstInput = false;
            break;
        case 'C': displayText.setText("");
                  numStr1 = "";
                  numStr2 = "";
                  firstInput = true;
        }
    }
    private String evaluate()
    {
        final char beep = '\u0007';

        try
        {
            int num1 = Integer.parseInt(numStr1);
            int num2 = Integer.parseInt(numStr2);
            double result = 0;

            switch (op)
            {
                case '+': result = num1 + num2;
            }
            return String.valueOf(result);
        }
        catch (ArithmeticException e)
        {
            System.out.print (beep);
            return " E R R O R:" + e.getMessage();
        }
        catch (NumberFormatException e)
        {
            System.out.print (beep);
            if (numStr1.equals(""))
               return "E R R O R: Invalid First Number";
            else
               return "E R R O R: Invalid Second Number";
        }
        catch (Exception e)
        {
            System.out.print (beep);
            return "E R R O R";
        }
    }
    public static void main(String[] args)
    {
        caculatorButtons C = new caculatorButtons();
    }
}

Reply With Quote
  #2  
Old January 10th, 2007, 09:03 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Your evaluate method needs more case's.

Code:
            switch (op)
            {
                case '+': result = num1 + num2; break;
                case '-': do something; break;
                case '*': do something; break;
                case '/': do something; break;
            }
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Quick Help With Calculator


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT