943,510 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1129
  • Java RSS
Oct 22nd, 2007
0

Help with GUI

Expand Post »
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);
      }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PL.P27 is offline Offline
17 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Help with GUI

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
java Syntax (Toggle Plain Text)
  1. if(firstDenominator.getText().equals(""){
  2. //handle null value
  3. }
hope that helps
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 23rd, 2007
0

Re: Help with GUI

Thanks a lot, what if they enter letters, how would I change the if statement?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PL.P27 is offline Offline
17 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Help with GUI

hmm,
There is no clean way i can think of, but you can do
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 23rd, 2007
0

Re: Help with GUI

also how about if they enter a decimal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PL.P27 is offline Offline
17 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Help with GUI

You can do Double.parseDouble instead of Integer.parseInt
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 23rd, 2007
0

Re: Help with GUI

Thanks a lot everything really helped. I appreciate the time that you have given to help me. May you have a good day!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PL.P27 is offline Offline
17 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Help with GUI

you too :-)
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Array in Dice Program
Next Thread in Java Forum Timeline: any one have idia about this.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC