Help with GUI

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 17
Reputation: PL.P27 is an unknown quantity at this point 
Solved Threads: 0
PL.P27 PL.P27 is offline Offline
Newbie Poster

Help with GUI

 
0
  #1
Oct 22nd, 2007
Hi everybody, I'm new to GUI and was wondering if someone could help me with my fraction calculator if they input text in the fields or if they leave something blank. I'm not quite sure how to do that. Here's my code, and if someone could guide me to how to do it, I'd be very thankful.
fractionNumeratorOne = (int)(Integer.parseInt(firstNumerator.getText()));
      fractionDenominatorOne = (int)(Integer.parseInt(firstDenominator.getText()));
      fractionNumeratorTwo = (int)(Integer.parseInt(secondNumerator.getText()));
      fractionDenominatorTwo = (int)(Integer.parseInt(secondDenominator.getText()));
      if (fractionDenominatorOne==0 || fractionDenominatorTwo==0)
      {
        JOptionPane.showMessageDialog(null, "Zero isn't the right number, it will equal a positive infinity","Error", JOptionPane.ERROR_MESSAGE);
       
      }
      if(fractionNumeratorOne==)      {
      	JOptionPane.showMessageDialog(null, "Zero isn't the right number, it will equal a positive infinity","Error", JOptionPane.ERROR_MESSAGE);
      }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 198
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Help with GUI

 
0
  #2
Oct 23rd, 2007
basically you need to check if they have a value. if the enter nothing then the string will be null or "". If you try to set a integer equal to null, you will get an exception, so you need to check the value of the fraction inputs before you turn them into integers.
do so this simly use an if statement
  1. if(firstDenominator.getText().equals(""){
  2. //handle null value
  3. }
hope that helps
this.love(*);
&hea/rts;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: PL.P27 is an unknown quantity at this point 
Solved Threads: 0
PL.P27 PL.P27 is offline Offline
Newbie Poster

Re: Help with GUI

 
0
  #3
Oct 23rd, 2007
Thanks a lot, what if they enter letters, how would I change the if statement?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 198
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Help with GUI

 
0
  #4
Oct 23rd, 2007
hmm,
There is no clean way i can think of, but you can do
  1. try{
  2. int num=Integer.parseInt(firstDenominator.getText());
  3. //if it gets to here its valid
  4. }catch(NumberFormatException e){
  5. //invalid number
  6. }
Last edited by nschessnerd; Oct 23rd, 2007 at 12:53 am.
this.love(*);
&hea/rts;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: PL.P27 is an unknown quantity at this point 
Solved Threads: 0
PL.P27 PL.P27 is offline Offline
Newbie Poster

Re: Help with GUI

 
0
  #5
Oct 23rd, 2007
also how about if they enter a decimal
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 198
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Help with GUI

 
0
  #6
Oct 23rd, 2007
You can do Double.parseDouble instead of Integer.parseInt
this.love(*);
&hea/rts;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 17
Reputation: PL.P27 is an unknown quantity at this point 
Solved Threads: 0
PL.P27 PL.P27 is offline Offline
Newbie Poster

Re: Help with GUI

 
0
  #7
Oct 23rd, 2007
Thanks a lot everything really helped. I appreciate the time that you have given to help me. May you have a good day!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 198
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Help with GUI

 
0
  #8
Oct 23rd, 2007
you too :-)
this.love(*);
&hea/rts;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC