0

I am a making an app that calculates the future value of a given amount of money.enter image description here

This is what my program looks like and I a trying to find out how I can validate the monthly payment, and yearly interest rate fields of my program. If the user clicks enter and does not input anything in monthly payment, interest rate, or both I would like the program to prompt the user that those are required fields.

This is the part of the code that outputs the data and process it. I have attempted to create a idValidData class that checks for valid data, but I don't know what to do with it.

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    DefaultListModel futureValueListModel = new DefaultListModel();

    if (source == exitButton)
    {
        System.exit(0);
    }
    else if (source == calculateButton)
    {       
        try
        {

        double payment = Double.parseDouble(paymentTextField.getText());
        double rate = Double.parseDouble(rateTextField.getText());
        int years = (yearsComboBox.getSelectedIndex()) + 1;
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        for(int i = 1; i <= years; i++)
        {       
            double futureValue = FinancialCalculations.calculateFutureValue(payment, rate, i);
            futureValueListModel.addElement("Year " + i + ": " + currency.format(futureValue));
        }  
        futureValueList.setModel(futureValueListModel);
        }
        catch(Exception ee)
        {
            System.out.println("This program ended because you didn't enter any data."); //when the user doesn't input data and clicks enter, the program will throw this exception
        }

    }
 }
 //END OF ACTION PERFORMED METHOD

public boolean isValidData() //VALIDATES THE DATA
{
    SwingValidator sv = new SwingValidator();
    if (sv != null)
    {
        return sv.isPresent(paymentTextField, "Monthly Payment")
            && sv.isPresent(rateTextField, "Yearly Interest Rate");
    }       
    else{
        return sv.isPresent(rateTextField, "Yearly Interest Rate");
        }
}

I also have a swing validator class that will return booleans when it checks for data entry, and data type. The code I have for it is:

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.math.BigDecimal;

public class SwingValidator
{
    public static boolean isPresent(JTextComponent c, String title)
    {
        if (c.getText().length() == 0)
        {
            showMessage(c, title + " is a required field.\n"+ "Please re-enter.");
            c.requestFocusInWindow();
            return false;
        }
        return true;
    }

    public static boolean isInteger(JTextComponent c, String title)
    {
        try
        {
            int i = Integer.parseInt(c.getText());
            return true;
        }
        catch (NumberFormatException e)
        {
            showMessage(c, title + " must be an integer.\n"+ "Please re-enter.");
            c.requestFocusInWindow();
            return false;
        }
    }

    public static boolean isDouble(JTextComponent c, String title)
    {
        try
        {
            double d = Double.parseDouble(c.getText());
            return true;
        }
        catch (NumberFormatException e)
        {
            showMessage(c, title + " must be a valid number number.\n"+ "Please re-enter.");
            c.requestFocusInWindow();
            return false;
        }
    }

    private static void showMessage(JTextComponent c, String message)
    {
            JOptionPane.showMessageDialog(c, message, "Invalid Entry",JOptionPane.ERROR_MESSAGE);
    }


}
Eden
  • 45
  • 5
  • 1
    What's the problem? It looks like you already have validation conditions in your code. Is something not working? – Michael Queue Nov 28 '15 at 18:55
  • Does the accepted answer here help: http://stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers – user2390182 Nov 28 '15 at 18:55
  • I would like the data validation to occur in the actionPerformed method, but I'm not 100% sure how to implement that. Is it possible to get rid of the isValid method, and just instantiate the SwingValidator in the actionPerformed method? – Eden Nov 28 '15 at 19:03
  • 1
    You could have a look at [Validating Input](http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification). You might also consider using a `JSpinner` or `JFormattedTextField` which are designed to deal with, among others, integer values – MadProgrammer Nov 28 '15 at 20:12

0 Answers0