tags:

views:

116

answers:

2

hi.

i'm making a calculator and need to get my buttons to work but i don't know what to do.

i have 2 ways of inserting numbers. 1) physically write the number in the field 2) push a button and the corresponding number should appear in the field.

i don't need the +-*/ and only 1 input is needed. when = is clicked my formula (number -1 *1.20 +2.80) is executed.

i've got everything else to work. i can type in a number and get an answer but i can't figure out how to get the buttons to do their job.

any help would be appreciated.

thanks

+3  A: 

Your "formula", e.g. 1*1.20+2.80 is called an expression. It's most likely a String that you input somehow.

The "classic" way to do this is to parse the String into tokens, which in your case will be either numbers or operators. Then an expression evaluator chews on the stream of tokens and comes up with a result you can display.

This is a fair bit of work. Other people have already done it, fortunately. One example I found by Googling for "java expression evaluator" is this: http://lts.online.fr/dev/java/math.evaluator/

It does a lot more than you need. You can either use it as is or fiddle with it first.


Update: Getting number buttons to work.

String fieldContents = "";

JTextField field = new JTextField;

ActionListener acLi = new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    JButton btn = (JButton) ae.getSource();
    fieldContents += btn.getText();
    field.setText(fieldContents);
  }
};

JButton button0 = new JButton("0");
button0.addActionListener(acLi);
...
JButton button9 = new JButton("9");
button9.addActionListener(acLi);

That should mostly do it. Enjoy!

Carl Smotricz
i've got the expression to work i just need the buttons e.g 1,2,3 to put 1 2 3 etc in the field.i'll have a look at the site
Tuffy G
You're not giving us a heck of a lot of code to work with! See upcoming update in my answer.
Carl Smotricz
at the moment the only bit of code for the button i've got isprivate javax.swing.Jbutton btn0;and private void btn0ActionPerformed(java.awt.event.ActionEvent evt)
Tuffy G
I don't enjoy being mean (actually, yes I do) but wouldn't it have been a good idea to learn the basics of Java programming *before* taking on this job?
Carl Smotricz
Look here for more hints: <http://www.beginner-java-tutorial.com/java-swing-calculator.html> .
Carl Smotricz
thats what i'm doing lol.the taxi company is mine loli wanted to learn java and apart from a hello world i didn't know what to do so i began with this. it'll help us and i can learn java at same time..... simples lol..i don't mean to sound daft but could you please expalin whats happening with that bit of code?
Tuffy G
I have to go fix my in-laws' printer... Read this: <http://java.sun.com/docs/books/tutorial/uiswing/> You probably only need to go as far as "Using Swing Components". Good luck!
Carl Smotricz
lol thanks for ypur help
Tuffy G
A: 

thanks for the help.

i finally figured out a way how to do it, all i did was add a set and get text

Tuffy G