GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Please Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Please Help" Watch "Please Help" New topic
Author

Please Help

Rich Nello
Greenhorn

Joined: Dec 08, 2003
Posts: 16
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


Rich Nello (Java Greenhorn)
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281

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.


The nice thing about Standards is that there are so many to choose from!
Rich Nello
Greenhorn

Joined: Dec 08, 2003
Posts: 16
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help