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);
      }

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

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

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
}

also how about if they enter a decimal

You can do Double.parseDouble instead of Integer.parseInt

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

you too :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.