2

I'm new here, and I'd like some help on a small Java project I'm doing. This is the code snippet I need help with:

private void CalculateButtonActionPerformed(java.awt.event.ActionEvent evt)                                                
{                                                    
    // TODO add your handling code here:
    float Principal, Rate, Time, Result, Temp;

    Principal = Float.valueOf(PrincipalTextField.getText());
    Rate = Float.valueOf(RateTextField.getText());
    Time = Float.valueOf(TimeTextField.getText());

    Temp = (float) Math.pow((1 + Rate / 100), Time);
    Result = Principal * Temp;
    ResultTextField.setText(String.valueOf(Result));
}

I'd like to check if PrincipalTextField, OR RateTextField, OR TimeTextField aren't filled by the user, and if so, display a dialog box that asks him/her to recheck them. The text fields are JFormattedTextField variables. I realise that I can do this with a if/else or a while loop, but I'm not sure how to set about doing so. Please help!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
SRSR333
  • 187
  • 4
  • 15

3 Answers3

3

You can do something like this:

  • The getText() returns you a String value. So you can always invoke length() and check whether the length comes to 0 or not. (*I would suggest calling trim() on the String before calling length() to remove any whitespaces)

  • Next if any of the length comes to be zero, what you want to do is display a Dialog Box. This you can do by calling JOptionPane.showMessageDialog(). You can read more about "How to Make Dialogs" over here.

So, you would do something like this:

String principalText = PrincipalTextField.getText();
String rateText = RateTextField.getText();
String timeText = TimeTextField.getText();

if(principalText.trim().length == 0 || rateText.trim().length == 0 || timeText.trim().length == 0){
    JOptionPane.showMessageDialog(null, "YOUR_ERROR_MSG", "ERROR_TITLE", JOptionPane.ERROR_MESSAGE);
}

This might be off-topic, but I would suggest looking at Java Naming Convention. The convention for variables is to compose variable names using mixed case letters starting with a lower case letter

Sujay
  • 6,753
  • 2
  • 30
  • 49
  • Ah, thank you. I had experimented with the trim() and length() methods, but it seems that I didn't use them properly. The thing about naming convention is new... Isn't it only methods that start with lower case letters and then uppercase, as in "showMessageDialog"? I'm also learning Objective-C, where variables are capitalised the way I've done it. Maybe I just got confused. But thank you very much. – SRSR333 Sep 08 '12 at 15:16
  • @user1654223: Nope, variables also follow the same pattern. But glad to help :) – Sujay Sep 08 '12 at 15:18
2
  1. you miss reason for why there is JFormattedTextField

  2. have to set Number Formatter for JFormattedTextField, then

    • you not need to parsing Float value (better could be to use double)

    • empty coudl be 0 (zero) value by default

    • take value in the form ((Number)PrincipalTextField.getValue()).floatValue();

  3. look at code example for tutorial,

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Also consider subclassing InputVerifier, as discussed in Validating Input. There's a related example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045