Join 149,780 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,669 people online right now. Registration is fast and FREE... Join Now!
Hi all, I was writing an applet that calculates the square root it is not finished yet cuz i have some(read MANY) problems. At this moment I want the values of X1 and X2 to be displayed in a messageDialog Box. Also I get possible loss of precision warning when I compile. help
a = Double.parseDouble(aa.getText()); //when I compile get here possible loss of precision b = Double.parseDouble(bb.getText()); //possible loss of precision c = Double.parseDouble(cc.getText()); //possible loss of precision
X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision
calc.addActionListener(this);
} public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2); }
Hell(o) I did what ericode told me it helped about that issue.Thx. But now I get different kind of problem(s). JVM says:
QUOTE
java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Kv.init(Kv.java:35) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Exception in thread "thread applet-Kv.class" java.lang.NullPointerException at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source) at sun.plugin.AppletViewer.showAppletException(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
a = Double.parseDouble(A.getText()); //when I compile get here possible loss of precision b = Double.parseDouble(B.getText()); //possible loss of precision c = Double.parseDouble(C.getText()); //possible loss of precision
X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision
calc.addActionListener(this);
} public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2); }
The problem is that your text fields don't have any data in them yet when you try to calculate the roots. To make sure data is in them, move your root calculating portion of code into the actionPerformed method.
Here is your code, I just cut and pasted the numerical calculation part into the actionPerformed method:
} public void actionPerformed(ActionEvent e) { a = Double.parseDouble(A.getText()); //when I compile get here possible loss of precision b = Double.parseDouble(B.getText()); //possible loss of precision c = Double.parseDouble(C.getText()); //possible loss of precision
X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a); //possible loss of precision
JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2); }
SO, I did some Exception handling after reading the tutorials by William_Wilson and got stuck.
I want to throw an Exception if any of the TextFields are emty and notify the user to enter a number.
When I compile I get:
QUOTE
actionPerformed(java.awt.event.ActionEvent) in Kv cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; overridden method does not throw Kv.Isklucok
if(D>0) JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2); else if(D<0) JOptionPane.showMessageDialog(this, "the numbers are komplex"); else JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2); }
OK, I modified the code but problems will be problems. I don't have compiling issues but I still have issues with Exception handling and with the RESET Button. The thing is that RESET does the same thing as CALCULATE. I know it has to do with that they use the same actionPerformed() method but I don't know other way.
if(D>0) JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2); else if(D<0) JOptionPane.showMessageDialog(this, "the numbers are komplex"); else JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2); } }
if(D>0) JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2); else if(D<0) JOptionPane.showMessageDialog(this, "the numbers are komplex"); else JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2); }