0

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);
       
      }
    [B]  if(fractionNumeratorOne==)[/B]      {
      	JOptionPane.showMessageDialog(null, "Zero isn't the right number, it will equal a positive infinity","Error", JOptionPane.ERROR_MESSAGE);
      }
2
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by nschessnerd
0

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

if(firstDenominator.getText().equals(""){
//handle null value
}

hope that helps

0

Thanks a lot, what if they enter letters, how would I change the if statement?

0

hmm,
There is no clean way i can think of, but you can do

try{
int num=Integer.parseInt(firstDenominator.getText());
//if it gets to here its valid
}catch(NumberFormatException e){
//invalid number
}
0

Thanks a lot everything really helped. I appreciate the time that you have given to help me. May you have a good day!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.