java - Compile/ Catch an exception -


i having lot of trouble code. code compiled , ran suppose before tried put in code catch exception. no longer compile. suppose catch exception , produce error message if user inputs negative number.

import java.awt.event.actionevent; //next group of lines import various java classes import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jcheckbox; import javax.swing.jpanel; import javax.swing.jtextfield; import java.awt.gridlayout; import java.text.*; import java.io.ioexception;  public class squareroot extends jframe {     public static void main(string[] args)  {         //creates window         jframe frame = new jframe();         frame.setsize(450, 300);         frame.settitle("find square root");         frame.setdefaultcloseoperation(jframe.exit_on_close);          jlabel numberlbl = new jlabel("enter number:");         final jtextfield numberfield = new jtextfield(10);         numberfield.settext("");          jlabel answerlbl = new jlabel("square root of number is:");         final jtextfield answerfield = new jtextfield(10);         answerfield.settext("");          jlabel buttonlabel = new jlabel("calculate square root");         jbutton button = new jbutton("find square root");          jpanel panel = new jpanel();         panel.setlayout(new gridlayout(3,2));         panel.add(numberlbl);         panel.add(numberfield);         panel.add(buttonlabel);         panel.add(button);         panel.add(answerlbl);         panel.add(answerfield);         frame.add(panel);          class calculatelistener implements actionlistener {              public void actionperformed(actionevent event) {                  double numberx = double.parsedouble(numberfield.gettext());                 try                 {                     if(numberx >=0);                 }                 catch(ioexception e)                 {                     system.out.println("number can not negative.");                 }                 double answer = math.sqrt(numberx);                 answerfield.settext(string.valueof(answer));              }         }          actionlistener listener = new calculatelistener();         button.addactionlistener(listener);          frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);          }     } 

you not need try-catch check negative, below should enough. may need try-catch when formatting.

double nox = 0; try {    nox = double.parsedouble(numberfield.gettext()); } catch(numberformatexception e) {    system.out.println("not valid number"); } if(nox < 0) {    system.out.println("number can not negative."); } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -