• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help

 
Rich Nello
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new to applets, AWT, GUI and Java in general so please forgive me if I don't manage to communicate my problem as well as i should.
Like I said, AWT graphics and applets are really new to me! We never covered them in class. However, with a bit of pain, I managed to modify a simple applet calculator but adding new buttons to it and changing abit of the code where i thought was necessary! However I am stuck on the last button so i really hope one of you can help me!
I have a textField called 'txtTotal' which acts as my calculator's display area. before numbers are displayes however, they are being converted form Double integer variables to Strings.
the main variables are as follows:
strnumtemp // String
numtemp // Double
strnum // String
num // Double
counter // int
I have successfuly managed to create a few buttons like for example the 'Sqauare Root' Button. Functionality for this button is seen in the code below:
public void actionPerformed(ActionEvent e)
{
for(int i = 0;i <= 9; i++)
{
if (e.getSource() == button[i])
{
play(getCodeBase(),i + ".au");
strnum += Integer.toString(i);
txtTotal.setText(strnum);
num = Double.valueOf(strnum).doubleValue();
}
}
if (e.getSource() == root)
{
double x =Math.sqrt(num);
strnumtemp = Double.toString(x);
txtTotal.setText(strnumtemp);
num = Double.valueOf(strnumtemp).doubleValue();
}

Now, my only problem is this: The 'Decimal Point' Button (Code below)
if (e.getSource() == decpoint)
{
double z = num;
strnumtemp = Double.toString(z);
txtTotal.setText(txtTotal.getText() + ".");
}
If I input a number and then I hit the decimal point button, I see it (which confirms to me that the button is working and doing what it should), but the problem is that once i type in the next number (which should ideally follow after the decimal point), the decimal disappears and the number I type in adds on to the existing number ... but without the decimal!
Could anyone guide me as to what i'm doing wrong please? I realy can't seem to manage on my own.
Thank You
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are adding the number to strnum, not the contents of the textfield txtTotal, so the dot gets overwritten. Maybe you should add the decimal point to strnum further down where you handle the decimal point or get the txtTotal contents here.
 
Rich Nello
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eddie Vanda:
You are adding the number to strnum, not the contents of the textfield txtTotal, so the dot gets overwritten. Maybe you should add the decimal point to strnum further down where you handle the decimal point or get the txtTotal contents here.


Cheers Eddie!
Thanks a million! You were 100% spot on!
Like you pointed out, this was what solved my problem: "... or get the txtTotal contents here."
Changed the code to this:

and left all the rest as is! The decimal point does not get overwritten any more and my calculator is working fine!
Thanks again!
I really appreciate your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic