swing - I cant get my try catch to work in java -


so here gui can see.

import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jtextfield; import javax.swing.jbutton; import javax.swing.jlabel; import java.awt.event.actionlistener; import java.awt.event.actionevent;  public class frame1 {      private jframe frame;     private jtextfield textfieldnum1;     private jtextfield textfieldnum2;     private jtextfield textfieldans;     private termostat thermo;      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             public void run() {                 try {                     frame1 window = new frame1();                     window.frame.setvisible(true);                 } catch (exception e) {                     e.printstacktrace();                 }             }         });     }      public frame1() {         initialize();     }      private void initialize() {         thermo = new termostat();          frame = new jframe();         frame.setbounds(100, 100, 696, 250);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.getcontentpane().setlayout(null);          textfieldnum1 = new jtextfield();         textfieldnum1.setbounds(176, 11, 147, 46);         frame.getcontentpane().add(textfieldnum1);         textfieldnum1.setcolumns(10);          textfieldnum2 = new jtextfield();         textfieldnum2.setbounds(176, 154, 147, 46);         frame.getcontentpane().add(textfieldnum2);         textfieldnum2.setcolumns(10);          jbutton btnnewbutton = new jbutton("convert celcius");         btnnewbutton.addactionlistener(new actionlistener() {             public void actionperformed(actionevent arg0) {                 double myf = thermo.converttocelcius(double.parsedouble(textfieldnum1.gettext()));                 textfieldans.settext(string.valueof(myf));             }         });         btnnewbutton.setbounds(0, 0, 171, 68);         frame.getcontentpane().add(btnnewbutton);          jbutton btnnewbutton_1 = new jbutton("convert fahrenheit");         btnnewbutton_1.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 double myf = thermo.converttofahrenheit(double.parsedouble(textfieldnum2.gettext()));                 textfieldans.settext(string.valueof(myf));             }         });         btnnewbutton_1.setbounds(0, 143, 171, 68);         frame.getcontentpane().add(btnnewbutton_1);          textfieldans = new jtextfield();         textfieldans.setbounds(354, 90, 147, 46);         frame.getcontentpane().add(textfieldans);         textfieldans.setcolumns(10);          jlabel lblnewlabel = new jlabel("converted");         lblnewlabel.setbounds(285, 94, 112, 38);         frame.getcontentpane().add(lblnewlabel);     } } 

and here comes problem. when made class wanna run input in calculate celcius fahrenheit, dont want crash when type else numbers. cant work need guys try catch work. im thankful get.

import javax.swing.joptionpane;  public class termostat {     public double converttocelcius(double input) {         double far = 0;         try {             far = (input - 32) * 5 / 9;         } catch (exception e) {             joptionpane.showmessagedialog(null, "wrong input");             return far;         }         return far;     }      public double converttofahrenheit(double input) {         double cel = 1;         try {             cel = (input * 9 / 5) + 32;         } catch (exception e) {             joptionpane.showmessagedialog(null, "wrong input");             return cel;         }         return cel;      } } 

this line exception:

     double myf = thermo.converttofahrenheit(double.parsedouble(textfieldnum2.gettext())); 

so guard numberformatexception (parsedouble).


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -